循环内的set-interval和clear-interval

时间:2013-02-26 21:44:13

标签: javascript for-loop setinterval clearinterval

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

2 个答案:

答案 0 :(得分: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); }

答案 1 :(得分: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>