javascript - setinterval seems to accelerate -


i have setinterval within function, on first click seems fine, every subsequent click accelerates timer. don't understand why.

function startgame() {     var myvar=setinterval(function(){mytimer()},100); } 

when clicking second time, first interval works, because of event happens twice in 100ms. try this:

var myvar; function startgame() {     if(myvar)         clearinterval(myvar);     myvar=setinterval(function(){mytimer()},100); } 

or:

var myvar; function startgame() {     if(!myvar)         myvar=setinterval(function(){mytimer()},100); } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -