admin管理员组

文章数量:1023044

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

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

本文标签: