2017-07-06 92 views
-1

如何等待每10個循環1或2分鐘?Javascript - 每10個循環等待1分鐘?

舉例來說,這是我的工作代碼:

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 
+0

會發生什麼事,當你運行該代碼? – JeffUK

+0

「counter」變量在哪裏被定義,它的值是什麼? – Ionut

+0

@Ionut我更新了我的問題。謝謝。 – laukok

回答

1

您可以使用setInterval,並且在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: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)

+0

感謝您的回答!這是不是也在等待前10秒2秒? – laukok

+1

@ teelou yep ..... –

+0

很酷。是否有可能不在第一個10循環等待? – laukok

1

試圖把你想每分鐘執行一次的setTimeout函數內部的功能。此外,如果您想要執行多次,則應該使用setInterval替換setTimeout

setInterval(function() { 
    dates.forEach(function(date, index) { 
     counter ++; 

     // Reset when you reach 10 counts. 
     if (counter === 10) { 
     counter = 0; 
     } 
    } 
}, 120000); 
0

您可以在超時時間內擁有變量。當你的計數器達到10時,將它的值設置爲120000.並確保你在IIFE中包含settimeout(立即調用函數表達式)。所以那個時候對於特定的索引,它會等待2分鐘&然後繼續下一個索引的循環。

+0

請舉例嗎? – laukok