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

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -