2013-02-26 101 views
0

我在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]; });

+0

什么是你的代码试图做? – 2013-02-26 21:45:11

+0

它看起来像经典的循环和settimeout问题。请参阅http://stackoverflow.com/questions/14791158/javascript-settimeout-and-loops – 2013-02-26 21:46:32

+3

任何时候当你发现自己在一个技术问题中写下“......它不工作......”,退后一步,并说*完全*你期望它做什么,*完全*它在做什么,以及为什么你认为这是不正确的。 – 2013-02-26 21:46:49

回答

0

好像你有你的逻辑倒退。每当当前时间等于timers中的某个时间时,您想提醒用户,对不对?为什么不使用setInterval()并跳过for()循环?如果他们已经排序,这工作得很好。此外,似乎一个if()将做一个更简单的“比较时间每秒”的方法。

我放慢了这个演示过程,使其明显。

演示:jsFiddle

脚本:

var timers = [ 
     [new Date((new Date()).getTime() + 3000),'party!'], 
     [new Date((new Date()).getTime() + 6000), 'sleep'] 
    ], 
    timer = setInterval(checkTime, 300); 

function checkTime() { 
    if(timers.length) { 
     if (parseInt(timers[0][0].getTime()/1000) 
      == parseInt(new Date().getTime()/1000)) { 

      //alert here 
      document.getElementById('result').insertAdjacentHTML(
       'beforeEnd', 
       timers[0][0] + ": " + timers[0][1] + '<br />' 
      ); 
      timers.shift(); 
     }; 
    } else { 
     clearInterval(timer); 
    }; 
}; 

HTML:

Wait 3 seconds... 
<div id="result"></div> 
+0

我不想每次循环访问数组。该数组已经排序(即,第一个元素应该是第一个提醒,第二个元素将是下一个),但唯一的问题是,它应该只在当前时间与数组中的时间匹配时才会提醒。 – user1846348 2013-02-26 22:37:29

+0

@ user1846348创建许多定时器没有多大意义。为什么不只是'.shift()'并且完全跳过'for()'循环。我更新了我的答案。 – ThinkingStiff 2013-02-26 22:45:12

+0

感谢它的工作! – user1846348 2013-02-26 23:51:17

0

也许是:

var timeCheck = {}; 
function matchTime() { 
    for (var i=0;i<timers.length;i++) { 
     timeCheck[i] = 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(i); } 
      } 
     } 
    }, 10); 
    } 
} 

function stopCheck(i) { clearInterval(timeCheck[i]); } 

或者是:

var timeCheck = 0; 
function matchTime() { 
    var timeCheck = setInterval(function() { 
    for (var i=0;i<timers.length;i++) { 
     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(i); } 
      } 
     } 
    } 
    }, 10); 
} 

function stopCheck(i) { clearInterval(timeCheck); } 
+0

第一个不工作。第二个是给我一个数组中的第一个元素的警报,但之后它没有给出任何警报。 (对于阵列中的第二个元素,它没有给出警报) – user1846348 2013-02-26 22:29:58