0

How can I change the global variable a every second using setInterval?

Tried this, but it not works as it will reset a's value when leaving setInterval.

var a=1;
var timerId = setInterval(function(){ 
     a++; 
     alert(a);
     if (a == 5) clearInterval(timerId);
}, 1000);
document.write(a);

Eventually, is there any better way to do it, other than setInterval?

neptune
  • 1,211
  • 2
  • 19
  • 32

0 Answers0