2013-02-26 31 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