我想在while循环中添加延迟/睡眠:
我试过这样的:
alert('hi');
for(var start = 1; start < 10; start++) {
setTimeout(function () {
alert('hello');
}, 3000);
}
Run Code Online (Sandbox Code Playgroud)
只有第一种情况是真的:显示后alert('hi'),它将等待3秒然后alert('hello')将显示,但随后alert('hello')将反复不断.
我想要的是,在alert('hello')3秒之后显示alert('hi')之后,它需要等待第二次3秒alert('hello'),依此类推.
在继续之前,我需要一个等待异步调用的循环.就像是:
for ( /* ... */ ) {
someFunction(param1, praram2, function(result) {
// Okay, for cycle could continue
})
}
alert("For cycle ended");
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?你有什么想法?