【发布时间】:2021-05-30 08:16:18
【问题描述】:
我想在 while 循环中添加延迟/睡眠:
我试过这样:
alert('hi');
for(var start = 1; start < 10; start++) {
setTimeout(function () {
alert('hello');
}, 3000);
}
只有第一种情况为真:显示alert('hi')后,等待3秒,然后显示alert('hello'),然后alert('hello')不断重复。
我想要的是,在alert('hello') 在alert('hi') 之后显示3 秒之后,它需要等待3 秒第二次alert('hello') 等等。
【问题讨论】:
-
for(var i=0; i
-
const setTimeOutFn= async()=>{ for(var start = 0; start { setTimeout(() => { console.log('hello', start); res() }, 3000); }) } }
标签: javascript loops sleep