Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js render loop that breaks once a condition is met

How do I set up a setInterval render loop that breaks, once a condition is met?

like image 922
davivid Avatar asked Sep 28 '10 16:09

davivid


1 Answers

You can store the interval ID and clear it via clearInterval(), for example

var timer = setInterval(myFunction, 1000);

function myFunction() {
  if(condition) {
    clearInterval(timer);
    return;
  }
  //do stuff
}

Or if you can just call clearInterval() where setting the condition, so the next interval doesn't run, having no logic for this in the function itself.

like image 135
Nick Craver Avatar answered Oct 15 '22 03:10

Nick Craver