3

現在の反復を中断して次の反復に進むために使用されるのと同じように、JavaScript でcontinue現在の反復を中断setInterval()し、待機せずに次の間隔に進むにはどうすればよいですか?

var intervalID = window.setInterval( function() {
   if(conditionIsTrue) {
      // Break this iteration and proceed with the next
      // without waiting for 3 seconds.
   }
}, 3000 );
4

2 に答える 2

2

「単純に」(またはそれほど単純ではない)間隔をクリアして、再作成できます。

// run the interval function immediately, then start the interval
var restartInterval = function() {
    intervalFunction();
    intervalID = setInterval(intervalFunction, 3000 );
};

// the function to run each interval
var intervalFunction = function() {
    if(conditionIsTrue) {
      // Break this iteration and proceed with the next
      // without waiting for 3 seconds.

      clearInterval(intervalID);
      restartInterval();
   }
};

// kick-off
var intervalID = window.setInterval(intervalFunction, 3000 );

ここにデモ/テストフィドルがあります。

于 2012-12-01T06:09:00.900 に答える