Emud Ron Emud Ron - 22 days ago 6
Javascript Question

Countdown gets stuck at 01:00

I created a countdown in jQuery but I have an issue. When the count reaches

01:00
it gets stuck instead of continuing on to
00:59
with minute of 0.

var start = $('#start');
var countMinutes = 2;

var timer;
start.on('click', function(event) {
event.preventDefault();
new Timer(function(val, countMinutes) {
timerMsg = (countMinutes >= 10 ? countMinutes : '0' + countMinutes) + ':' + (val >= 10 ? val : '0' + val);
time.text(timerMsg);
});
});

function Timer(callback, val, m) {
val = val || 59;
m = countMinutes;
timer = setInterval(function() {
callback(val, m);
if (val-- <= 0) {
m -= 1;
if (m < 1 && val <= 0) {

clearInterval(timer);
}
countMinutes = 0;
val += 60;
}
}, 1000);
}


my html.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="timer"><span id="time">10:00</span></h1>
<a href="#" id="start">Start</a>

Answer

Change

if (m < 1 && val <= 0) {

to

if (m < 0 && val <= 0) {