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); }

ありがとう。

私が解決しようとしているのは、現地時間がtimers配列(列0; timers [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()はるかに単純な「時間を秒ごとに比較する」方法で行うようです。

このデモでは、わかりやすくするためにプロセスの速度を落としました。

デモ: jsフィドル

脚本:

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 に答える