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
  • 40,609
  • 10
  • 73
  • 97
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758