0

我在 for 循环中有一个 set-interval 函数,如果在 set-interval 函数内满足条件,则会发出警报并清除间隔。下面是我的代码,但它不起作用任何人都可以告诉这里的错误是什么。

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 列;计时器 [count] [0]),我都需要收到警报。数组已经排序
timers.sort(function(a,b) { return a[0] - b[0]; });

4

2 回答 2

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); }
于 2013-02-26T22:02:31.620 回答
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>
于 2013-02-26T22:06:19.907 回答