davivid davivid - 1 year ago 78
Javascript Question

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?

Answer Source

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

var timer = setInterval(myFunction, 1000);

function myFunction() {
  if(condition) {
  //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.