我在for循環裏面有一個set-interval函數,如果符合條件,在set-interval函數裏面有set-interval函數給出警報並清除間隔。下面是我的代碼,但它不工作,任何人都可以告訴這裏的錯誤。set-interval和clear-interval for循環
var timeCheck = 0;
function matchTime() {
for (var i=0;i<timers.length;i++) {
timeCheck = setInterval(function() {
var theDate = new Date(timers[i][0]*1000);
var now = new Date();
if ((now.getFullYear() === theDate.getFullYear()) && (now.getMonth() === theDate.getMonth())) {
if ((now.getDate() === theDate.getDate()) && (now.getHours() === theDate.getHours())) {
if (now.getMinutes() === theDate.getMinutes() && (now.getSeconds() === theDate.getSeconds())) { alert("its Time for "+timers[i][1]); stopCheck(); }
}
}
}, 10);
}
}
function stopCheck() { clearInterval(timeCheck); }
謝謝。
我想解決的是:當本地時間與計時器數組(第0列;計時器[計數] [0])相匹配時,我需要每次都得到警報。數組已經排序timers.sort(function(a,b) { return a[0] - b[0]; });
什麼是你的代碼試圖做? – 2013-02-26 21:45:11
它看起來像經典的循環和settimeout問題。請參閱http://stackoverflow.com/questions/14791158/javascript-settimeout-and-loops – 2013-02-26 21:46:32
任何時候當你發現自己在一個技術問題中寫下「......它不工作......」,退後一步,並說*完全*你期望它做什麼,*完全*它在做什麼,以及爲什麼你認爲這是不正確的。 – 2013-02-26 21:46:49