0

I wonder if it actually is possible to stop function explicitly, after using setInterval on it.

<script>
setInterval(function(){document.write("Hello");},1000);
</script>
MattMoulson
  • 45
  • 1
  • 1
  • 4

1 Answers1

4

Use clearInterval() :

var timer = setInterval(function(){
      document.write("Hello");
      if (someCondition) {
         clearInterval(timer); // this stops the interval
      }
},1000);
canon
  • 38,844
  • 9
  • 71
  • 94
Denys Séguret
  • 355,860
  • 83
  • 755
  • 726