Javascript-每10个循环等待1分钟?
我怎样才能每10圈等待1或2分钟 例如,这是我的工作代码:Javascript-每10个循环等待1分钟?,javascript,loops,foreach,Javascript,Loops,Foreach,我怎样才能每10圈等待1或2分钟 例如,这是我的工作代码: var dates = ["2016-08-31T23:00:00.000Z","2016-09-01T23:00:00.000Z","2016-09-02T23:00:00.000Z","2016-09-03T23:00:00.000Z","2016-09-04T23:00:00.000Z","2016-09-05T23:00:00.000Z","2016-09-06T23:00:00.000Z","2016-09-07T23:00:
var dates = ["2016-08-31T23:00:00.000Z","2016-09-01T23:00:00.000Z","2016-09-02T23:00:00.000Z","2016-09-03T23:00:00.000Z","2016-09-04T23:00:00.000Z","2016-09-05T23:00:00.000Z","2016-09-06T23:00:00.000Z","2016-09-07T23:00:00.000Z","2016-09-08T23:00:00.000Z","2016-09-09T23:00:00.000Z","2016-09-10T23:00:00.000Z","2016-09-11T23:00:00.000Z","2016-09-12T23:00:00.000Z","2016-09-13T23:00:00.000Z","2016-09-14T23:00:00.000Z","2016-09-15T23:00:00.000Z","2016-09-16T23:00:00.000Z","2016-09-17T23:00:00.000Z","2016-09-18T23:00:00.000Z","2016-09-19T23:00:00.000Z","2016-09-20T23:00:00.000Z","2016-09-21T23:00:00.000Z","2016-09-22T23:00:00.000Z","2016-09-23T23:00:00.000Z","2016-09-24T23:00:00.000Z","2016-09-25T23:00:00.000Z","2016-09-26T23:00:00.000Z","2016-09-27T23:00:00.000Z","2016-09-28T23:00:00.000Z","2016-09-29T23:00:00.000Z","2016-09-30T23:00:00.000Z","2016-10-01T23:00:00.000Z"];
var counter = 0;
// Loop the dates and convert them to this format: yyyy-m-d
dates.forEach(function(date, index) {
counter ++;
console.log(date);
// Reset when you reach 10 counts.
if (counter === 10) {
counter = 0;
}
// Wait for 2 minute before the next 10 loop.
setTimeout( function() {
//
}, 120000);
});
可能吗?有什么想法吗
编辑:
我追求的是:
2016-08-31T23:00:00.000Z
2016-09-01T23:00:00.000Z
2016-09-02T23:00:00.000Z
2016-09-03T23:00:00.000Z
2016-09-04T23:00:00.000Z
2016-09-05T23:00:00.000Z
2016-09-06T23:00:00.000Z
2016-09-07T23:00:00.000Z
2016-09-08T23:00:00.000Z
2016-09-09T23:00:00.000Z
(wait for 2 minute here)
2016-09-10T23:00:00.000Z
2016-09-11T23:00:00.000Z
2016-09-12T23:00:00.000Z
2016-09-13T23:00:00.000Z
2016-09-14T23:00:00.000Z
2016-09-15T23:00:00.000Z
2016-09-16T23:00:00.000Z
2016-09-17T23:00:00.000Z
2016-09-18T23:00:00.000Z
2016-09-19T23:00:00.000Z
(wait for 2 minute here)
2016-09-20T23:00:00.000Z
2016-09-21T23:00:00.000Z
2016-09-22T23:00:00.000Z
2016-09-23T23:00:00.000Z
2016-09-24T23:00:00.000Z
2016-09-25T23:00:00.000Z
2016-09-26T23:00:00.000Z
2016-09-27T23:00:00.000Z
2016-09-28T23:00:00.000Z
2016-09-29T23:00:00.000Z
(wait for 2 minute here)
2016-09-30T23:00:00.000Z
2016-10-01T23:00:00.000Z
尝试将确实希望每分钟执行一次的函数放入setTimeout函数中。此外,如果要执行多次,则应将setTimeout替换为setInterval
setInterval( function() {
dates.forEach(function(date, index) {
counter ++;
// Reset when you reach 10 counts.
if (counter === 10) {
counter = 0;
}
}
}, 120000);
可以为超时时间设置变量。当计数器达到10时,将其值设置为120000。并确保将settimeout封装在IIFE(立即调用的函数表达式)中。因此,对于特定索引,它将等待2分钟,然后使用下一个索引继续循环。您可以使用
setInterval
,并在中为循环在每个间隔中增加10
var日期=[“2016-08-31T23:00:00.000Z”,“2016-09-01T23:00:00.000Z”,“2016-09-02T23:00:00.000Z”,“2016-09-03T23:00:00.000Z”,“2016-09-04T23:00:00.000Z”,“2016-09-05T23:00:00.000Z”,“2016-09-06T23:00:00.000Z”,“2016-09-09-07T23:00:00.000Z”,“2016-09-09-08T23:00:00:00:00.000Z”,“2016-09-093:00:00:00:00:00”,“2016-09-11T23:00:00.000Z”,“2016-09-12T23:00:00.000Z”,“2016-09-13T23:00:00.000Z”,“2016-09-14T23:00:00.000Z”,“2016-09-15T23:00:00.000Z”,“2016-09-16T23:00:00.000Z”,“2016-09-17T23:00:00.000Z”,“2016-09-18T23:00:00.000Z”,“2016-09-19T23:00:00.000Z”,“2016-09-09-203:00:00.000Z”,“2016-09-21T23:00:00:00”2016-09-23T23:00:00.000Z、2016-09-24T23:00:00.000Z、2016-09-25T23:00:00.000Z、2016-09-26T23:00:00.000Z、2016-09-27T23:00:00.000Z、2016-09-28323:00:00.000Z、2016-09-29T23:00:00.000Z、2016-09-30T23:00:00.000Z、2016-10-01T23:00:00.000Z];
var c=0
var time=2000;//2秒仅用于演示
函数loopDates(){
对于(变量i=c;i=dates.length)clearInterval(x)
}
循环日期()
var x=设置间隔(循环日期、时间)
运行此代码时会发生什么?定义的计数器在哪里?它的值是多少?@Ionut我更新了我的问题。谢谢。有例子吗?谢谢你的回答。前10秒也在等待2秒吗?@teelou我更新了我的答案,你可以取出函数并首先调用它。