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

如何在JavaScript循环中添加延迟?,javascript,loops,sleep,Javascript,Loops,Sleep,我想在while循环中添加延迟/睡眠: 我这样试过: alert('hi'); for(var start = 1; start < 10; start++) { setTimeout(function () { alert('hello'); }, 3000); } var TimedQueue = function(defaultDelay){ this.queue = []; this.index = 0; this.defaultDela

我想在
while
循环中添加延迟/睡眠:

我这样试过:

alert('hi');

for(var start = 1; start < 10; start++) {
  setTimeout(function () {
    alert('hello');
  }, 3000);
}
var TimedQueue = function(defaultDelay){
    this.queue = [];
    this.index = 0;
    this.defaultDelay = defaultDelay || 3000;
};

TimedQueue.prototype = {
    add: function(fn, delay){
        this.queue.push({
            fn: fn,
            delay: delay
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this
        , i = this.index++
        , at = this.queue[i]
        , next = this.queue[this.index]
        if(!at) return;
        at.fn();
        next && setTimeout(function(){
            self.next();
        }, next.delay||this.defaultDelay);
    },
    reset: function(){
        this.index = 0;
    }
}
loopOnArrayWithDelay(YourArray, 1000, 0, function(e, i){
    //Do something with item
}, function(i){
    //Do something once loop has completed
}
names = ['John', 'Ana', 'Mary'];

names.forEach((name, i) => {
 setTimeout(() => {
  console.log(name);
 }, i * 1000);  // one sec interval
});
警报('hi');
对于(var start=1;start<10;start++){
setTimeout(函数(){
警惕(“你好”);
}, 3000);
}
只有第一种情况是正确的:在显示
alert('hi')
后,它将等待3秒钟,然后将显示
alert('hello')
,但随后将不断重复显示
alert('hello')

我想要的是,在
alert('hello')
显示在
alert('hi')
之后3秒之后,它需要等待3秒,以便第二次
alert('hello')
等等。

该函数是非阻塞的,将立即返回。因此,您的循环将非常快速地迭代,它将快速连续地一个接一个地启动3秒超时触发器。这就是为什么您的第一个警报会在3秒钟后弹出,而所有其他警报都会毫不延迟地连续出现

您可能希望使用类似以下内容:

var i=1;//将计数器设置为1
函数myLoop(){//创建一个循环函数
setTimeout(函数(){//调用循环时调用3s setTimeout
console.log('hello');//您的代码在这里
i++;//递增计数器
如果(i<10){//如果计数器<10,则调用循环函数
myLoop();/…再次触发另一个
}//…设置超时()
}, 3000)
}
myLoop();//启动循环
该功能是非阻塞的,将立即返回。因此,您的循环将非常快速地迭代,它将快速连续地一个接一个地启动3秒超时触发器。这就是为什么您的第一个警报会在3秒钟后弹出,而所有其他警报都会毫不延迟地连续出现

您可能希望使用类似以下内容:

var i=1;//将计数器设置为1
函数myLoop(){//创建一个循环函数
setTimeout(函数(){//调用循环时调用3s setTimeout
console.log('hello');//您的代码在这里
i++;//递增计数器
如果(i<10){//如果计数器<10,则调用循环函数
myLoop();/…再次触发另一个
}//…设置超时()
}, 3000)
}

myLoop();//启动循环
尝试以下操作:

alert('hi');

for(var start = 1; start < 10; start++) {
  setTimeout(function () {
    alert('hello');
  }, 3000);
}
var TimedQueue = function(defaultDelay){
    this.queue = [];
    this.index = 0;
    this.defaultDelay = defaultDelay || 3000;
};

TimedQueue.prototype = {
    add: function(fn, delay){
        this.queue.push({
            fn: fn,
            delay: delay
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this
        , i = this.index++
        , at = this.queue[i]
        , next = this.queue[this.index]
        if(!at) return;
        at.fn();
        next && setTimeout(function(){
            self.next();
        }, next.delay||this.defaultDelay);
    },
    reset: function(){
        this.index = 0;
    }
}
loopOnArrayWithDelay(YourArray, 1000, 0, function(e, i){
    //Do something with item
}, function(i){
    //Do something once loop has completed
}
names = ['John', 'Ana', 'Mary'];

names.forEach((name, i) => {
 setTimeout(() => {
  console.log(name);
 }, i * 1000);  // one sec interval
});
var i=0,howManyTimes=10;
函数f(){
控制台日志(“hi”);
i++;
如果(i<多少次){
设置超时(f,3000);
}
}

f()尝试以下方法:

alert('hi');

for(var start = 1; start < 10; start++) {
  setTimeout(function () {
    alert('hello');
  }, 3000);
}
var TimedQueue = function(defaultDelay){
    this.queue = [];
    this.index = 0;
    this.defaultDelay = defaultDelay || 3000;
};

TimedQueue.prototype = {
    add: function(fn, delay){
        this.queue.push({
            fn: fn,
            delay: delay
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this
        , i = this.index++
        , at = this.queue[i]
        , next = this.queue[this.index]
        if(!at) return;
        at.fn();
        next && setTimeout(function(){
            self.next();
        }, next.delay||this.defaultDelay);
    },
    reset: function(){
        this.index = 0;
    }
}
loopOnArrayWithDelay(YourArray, 1000, 0, function(e, i){
    //Do something with item
}, function(i){
    //Do something once loop has completed
}
names = ['John', 'Ana', 'Mary'];

names.forEach((name, i) => {
 setTimeout(() => {
  console.log(name);
 }, i * 1000);  // one sec interval
});
var i=0,howManyTimes=10;
函数f(){
控制台日志(“hi”);
i++;
如果(i<多少次){
设置超时(f,3000);
}
}

f()另一种方法是将超时时间相乘,但请注意,这与睡眠不同。循环后的代码将立即执行,只有回调函数的执行被延迟

for (var start = 1; start < 10; start++)
    setTimeout(function () { alert('hello');  }, 3000 * start);
for(var start=1;start<10;start++)
setTimeout(函数(){alert('hello');},3000*start);

第一个超时将设置为
3000*1
,第二个超时设置为
3000*2
,依此类推。

另一种方法是将超时时间相乘,但请注意,这与睡眠不同。循环后的代码将立即执行,只有回调函数的执行被延迟

for (var start = 1; start < 10; start++)
    setTimeout(function () { alert('hello');  }, 3000 * start);
for(var start=1;start<10;start++)
setTimeout(函数(){alert('hello');},3000*start);

第一个超时将设置为
3000*1
,第二个超时将设置为
3000*2
,依此类推。

我想您需要这样的设置:

alert('hi');

for(var start = 1; start < 10; start++) {
  setTimeout(function () {
    alert('hello');
  }, 3000);
}
var TimedQueue = function(defaultDelay){
    this.queue = [];
    this.index = 0;
    this.defaultDelay = defaultDelay || 3000;
};

TimedQueue.prototype = {
    add: function(fn, delay){
        this.queue.push({
            fn: fn,
            delay: delay
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this
        , i = this.index++
        , at = this.queue[i]
        , next = this.queue[this.index]
        if(!at) return;
        at.fn();
        next && setTimeout(function(){
            self.next();
        }, next.delay||this.defaultDelay);
    },
    reset: function(){
        this.index = 0;
    }
}
loopOnArrayWithDelay(YourArray, 1000, 0, function(e, i){
    //Do something with item
}, function(i){
    //Do something once loop has completed
}
names = ['John', 'Ana', 'Mary'];

names.forEach((name, i) => {
 setTimeout(() => {
  console.log(name);
 }, i * 1000);  // one sec interval
});
测试代码:

var now = +new Date();

var x = new TimedQueue(2000);

x.add(function(){
    console.log('hey');
    console.log(+new Date() - now);
});
x.add(function(){
    console.log('ho');
    console.log(+new Date() - now);
}, 3000);
x.add(function(){
    console.log('bye');
    console.log(+new Date() - now);
});

x.run();
注意:使用警报会暂停javascript执行,直到关闭警报。
代码可能比您要求的要多,但这是一个健壮的可重用解决方案。

我认为您需要这样的解决方案:

alert('hi');

for(var start = 1; start < 10; start++) {
  setTimeout(function () {
    alert('hello');
  }, 3000);
}
var TimedQueue = function(defaultDelay){
    this.queue = [];
    this.index = 0;
    this.defaultDelay = defaultDelay || 3000;
};

TimedQueue.prototype = {
    add: function(fn, delay){
        this.queue.push({
            fn: fn,
            delay: delay
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this
        , i = this.index++
        , at = this.queue[i]
        , next = this.queue[this.index]
        if(!at) return;
        at.fn();
        next && setTimeout(function(){
            self.next();
        }, next.delay||this.defaultDelay);
    },
    reset: function(){
        this.index = 0;
    }
}
loopOnArrayWithDelay(YourArray, 1000, 0, function(e, i){
    //Do something with item
}, function(i){
    //Do something once loop has completed
}
names = ['John', 'Ana', 'Mary'];

names.forEach((name, i) => {
 setTimeout(() => {
  console.log(name);
 }, i * 1000);  // one sec interval
});
测试代码:

var now = +new Date();

var x = new TimedQueue(2000);

x.add(function(){
    console.log('hey');
    console.log(+new Date() - now);
});
x.add(function(){
    console.log('ho');
    console.log(+new Date() - now);
}, 3000);
x.add(function(){
    console.log('bye');
    console.log(+new Date() - now);
});

x.run();
注意:使用警报会暂停javascript执行,直到关闭警报。
它可能比您要求的代码更多,但这是一个健壮的可重用解决方案。

我可能会使用
setInteval
。像这样,

var period = 1000; // ms
var endTime = 10000;  // ms
var counter = 0;
var sleepyAlert = setInterval(function(){
    alert('Hello');
    if(counter === endTime){
       clearInterval(sleepyAlert);
    }
    counter += period;
}, period);

我可能会使用
setInteval
。像这样,

var period = 1000; // ms
var endTime = 10000;  // ms
var counter = 0;
var sleepyAlert = setInterval(function(){
    alert('Hello');
    if(counter === endTime){
       clearInterval(sleepyAlert);
    }
    counter += period;
}, period);

我使用Bluebird的
Promise.delay
和递归来实现这一点

函数myLoop(i){
返回承诺。延迟(1000)
.然后(函数(){
如果(i>0){
警惕(“你好”);
返回myLoop(i-=1);
}
});
}
myLoop(3)

我用蓝鸟的
承诺、延迟和递归来实现这一点

函数myLoop(i){
返回承诺。延迟(1000)
.然后(函数(){
如果(i>0){
警惕(“你好”);
返回myLoop(i-=1);
}
});
}
myLoop(3)

以下是我用于在阵列上循环的函数:

function loopOnArrayWithDelay(theArray, delayAmount, i, theFunction, onComplete){

    if (i < theArray.length && typeof delayAmount == 'number'){

        console.log("i "+i);

        theFunction(theArray[i], i);

        setTimeout(function(){

            loopOnArrayWithDelay(theArray, delayAmount, (i+1), theFunction, onComplete)}, delayAmount);
    }else{

        onComplete(i);
    }
}

下面是我用于在数组上循环的函数:

function loopOnArrayWithDelay(theArray, delayAmount, i, theFunction, onComplete){

    if (i < theArray.length && typeof delayAmount == 'number'){

        console.log("i "+i);

        theFunction(theArray[i], i);

        setTimeout(function(){

            loopOnArrayWithDelay(theArray, delayAmount, (i+1), theFunction, onComplete)}, delayAmount);
    }else{

        onComplete(i);
    }
}
在ES6(ECMAScript 2015)中,您可以使用延迟和间隔进行迭代

生成器是ECMAScript 6的一个新功能,是可以 停顿了一下,接着说。调用genFunc不会执行它。相反,它 返回一个所谓的生成器对象,该对象允许我们控制genFunc的 执行。genFunc()最初在其运行开始时挂起 身体。方法genObj.next()继续执行genFunc, 直到下一个产量。


代码示例:
让arr=[1,2,3,b'];
设genObj=genFunc();
设val=genObj.next();
console.log(val.value);
let interval=setInterval(()=>{
val=genObj.next();
如果(val.done){
间隔时间;
}否则{
console.log(val.value);
}
}, 1000);
函数*genFunc(){
用于(arr的let项目){
收益项目;
}
}
在ES6(ECMAScript 2015)中,您可以延迟迭代