Javascript - 每10个循环等待1分钟? [英] Javascript - wait for 1 minute for every 10 loop?

查看:171
本文介绍了Javascript - 每10个循环等待1分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何等待每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: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.000 Z  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月1日T23:00:00.000Z]; 

var counter = 0;

//循环日期并将它们转换为这种格式:yyyy-md
dates.forEach(function(date,index){
counter ++;

console.log(date);

//当你达到10次时重置
if(counter === 10){
counter = 0;
}

//在下一个10循环前等待2分钟
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.000 Z
2016-09-06T23:00:00.000Z
2016-09-07T23:00:00.000Z
2016-09-08T23:00:00.000Z
2016-09 -09T23:00:00.000Z

(等待2分钟)

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

(等待2分钟)

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.00 0Z
2016-09-27T23:00:00.000Z
2016-09-28T23:00:00.000Z
2016-09-29T23:00:00.000Z

(等待2分钟)

2016-09-30T23:00:00.000Z
2016-10-01T23:00:00.000Z


解决方案您可以使用 setInterval code> for 循环在每个时间间隔内增加10个数据元。

  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:0 0.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-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 c = 0var time = 2000; // 2秒仅用于demofunction loopDates(){for(var i = c; i  


How can I wait for 1 or 2 minute for every 10 loop?

For instance, this is my working code:

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);
});

Is it possible? Any ideas?

EDIT:

What I am after:

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

解决方案

You can use setInterval and inside just for loop to increment by 10 in each interval.

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 c = 0
var time = 2000; // 2 sec just for demo

function loopDates() {
  for (var i = c; i < c + 10; i++) {
    if (dates[i]) {
      var date = dates[i].slice(0, 10);
      console.log(date)
    }
  }
  c += 10

  if (c >= dates.length) clearInterval(x)
}

loopDates()
var x = setInterval(loopDates, time)

这篇关于Javascript - 每10个循环等待1分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆