相关疑难解决方法(0)

如何在JavaScript循环中添加延迟?

我想在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'),依此类推.

javascript sleep loops

313
推荐指数
13
解决办法
40万
查看次数

标签 统计

javascript ×1

loops ×1

sleep ×1