我在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