jquery - How to stop my javascript countdown by clicking a button? -
i have start button countdown, want stop when click stop button.
my html code:
<div> <span id="timer">25:00</span> </div>
start stop
my js code:
$('#starttimerbutton').click(function starter() { function starttimer(duration, display) { var timer = duration, minutes, seconds; setinterval(function () { minutes = parseint(timer / 60, 10); seconds = parseint(timer % 60, 10); minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.text(minutes + ":" + seconds); if (--timer < 0) { timer = duration; } }, 1000); } jquery(function ($) { var fiveminutes = 60 * 25, display = $('#timer'); starttimer(fiveminutes, display); }); });
first, try return interval in function:
function starttimer(duration, display) { var timer = duration, minutes, seconds; timeinterval = setinterval(function () { minutes = parseint(timer / 60, 10); seconds = parseint(timer % 60, 10); minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.text(minutes + ":" + seconds); if (--timer < 0) { timer = duration; } }, 1000); return timeinterval; }
then, calling function assign variable can clear later with:
var counttimeinterval = starttimer(); clearinterval(counttimeinterval);
Comments
Post a Comment