javascript - clearInterval does not work -


i'm trying create simple countdown timer. counts down number entered.

however, i'm trying clear interval when counter gets 0. @ moment seems acknowledge if statement, not clearinterval().

http://jsfiddle.net/tmyie/cf3hd/

$('.click').click(function () {     $('input').empty();     var rawamount = $('input').val();     var cleanamount = parseint(rawamount) + 1;      var timer = function () {          cleanamount--;          if (cleanamount == 0) {              clearinterval(timer);         }         $('p').text(cleanamount);     };      setinterval(timer, 500);  }) 

you're not saving return value of call setinterval, value needs passed clearinterval. passing timer handler no good.

var timer, timerhandler = function () {      cleanamount--;      if (cleanamount == 0) {          clearinterval(timer);     }     $('p').text(cleanamount); };  timer = setinterval(timerhandler, 500); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -