-1

This is my code for window.setInterval and window.clearInterval.

window.setInterval are work good, but when i tried to call window.clearInterval it's will show error intervalId is not defined

How can i do ?

.....................................................

<div onclick="start_setinterval('1')">START</div>
<div onclick="start_setinterval('0')">STOP</div>


<script>
  function start_setinterval(status) {
    var xxx = setInterval(function() {
      if (status == 1) {
        do_start_setinterval();
      } else {
        clearInterval(xxx);
      }
    }, 5000);
  }

function do_start_setinterval() {
  alert("test");
} 
</script>
Akrion
  • 18,117
  • 1
  • 34
  • 54

1 Answers1

3

The intervalId variable you are using to store a reference to the setInterval is limited in its scope to the function start_setinterval. You might need to assign it to window.intervalId as a quick way of fixing this code.

Raunaq Sachdev
  • 518
  • 4
  • 7