How to exit from setInterval

Martin Ongtangco picture Martin Ongtangco · Nov 25, 2009 · Viewed 77.5k times · Source

I need to exit from a running interval if the conditions are correct:

var refreshId = setInterval(function() {
        var properID = CheckReload();
        if (properID > 0) {
            <--- exit from the loop--->
        }
    }, 10000);

Answer

Christian C. Salvad&#243; picture Christian C. Salvadó · Nov 25, 2009

Use clearInterval:

var refreshId = setInterval(function() {
  var properID = CheckReload();
  if (properID > 0) {
    clearInterval(refreshId);
  }
}, 10000);