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
Post a Comment