Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Javascript - wait for 1 minute for every 10 loop?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...