有没有sleep比使用以下pausecomp函数更好的方法来设计JavaScript (从这里开始)?
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}
Run Code Online (Sandbox Code Playgroud)
这不是JavaScript中Sleep的重复- 动作之间的延迟 ; 我希望在函数中间实现真正的睡眠,而不是在执行代码之前的延迟.
fetch() {
return axios.get('/rest/foo')
//.then(response => {throw new Error(response)}) // Uncomment to test network error
//.then( <<add delay here>> ) // Uncomment to simulate network delay
}
Run Code Online (Sandbox Code Playgroud)
如何在后者中添加延迟然后阻塞,因此它会在将控制传递给提取调用者然后阻塞之前等待指定的时间量?