javascript - How to break current iteration and continue with next iteration within setInterval()? - Stack Overflow

Just like continue is used to break current iteration and proceed with the next, how can I break curren

Just like continue is used to break current iteration and proceed with the next, how can I break current iteration within setInterval() in JavaScript and proceed with the next interval without waiting?

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

Just like continue is used to break current iteration and proceed with the next, how can I break current iteration within setInterval() in JavaScript and proceed with the next interval without waiting?

var intervalID = window.setInterval( function() {
   if(conditionIsTrue) {
      // Break this iteration and proceed with the next
      // without waiting for 3 seconds.
   }
}, 3000 );
Share Improve this question edited May 15, 2021 at 15:40 Michael Rovinsky 7,2507 gold badges17 silver badges32 bronze badges asked Dec 1, 2012 at 5:59 Aniket SuryavanshiAniket Suryavanshi 1,5843 gold badges14 silver badges24 bronze badges 2
  • where is your iteration. Is there any loop? – polin Commented Dec 1, 2012 at 6:04
  • 2 Can you give us a little better idea of what you're trying to do. At the moment it sounds like you're trying to blow up your web browser. – Jamund Ferguson Commented Dec 1, 2012 at 6:18
Add a ment  | 

2 Answers 2

Reset to default 3

You could "simply" (or not so simply) clear the interval, and re-create it:

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

Here's a demo/test Fiddle.

Just tested this, and it acts as a continue statement in a loop. For fellow coders finding this now, just use return within the setInterval.

var myRepeater = setInterval( function() {
   if(conditionIsTrue) {
      return;
   }
}, 1000 );

EDIT: To ply with the immediate execution after breaking the current execution of the loop, one could instead do something like so (in theory. And beware of recursion issues if conditionIsTrue stays true):

function myFunction() {
    if(conditionIsTrue) {
       myFunction();
       return;
    }
    // Interval function code here...
}

var myRepeater = setInterval( myFunction, 1000 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745524536a4631410.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信