javascript - setInterval() won't stop if it gets called twice -


what i'm trying make slide out panels, here simplified example of problem i'm running into.

mycount = document.getelementbyid("counter") mycount.onclick = startcount; count = 0;  function startcount() {      timer = setinterval("counttoten()", 200);    }  function counttoten() {      count++;     mycount.innerhtml = count;      if (count >= 10) {         clearinterval(timer);     } } 

it works great if click once. if double click (and can't trust users click when they're supposed to), counter goes on forever. guess 2 timers got made, firebug showing timer have same id. how use clearinterval correctly when setinterval got called twice?

var timer; function startcount() {      if (!timer) {         timer = setinterval(counttoten, 200);     } } 

Comments