Reputation: 97
can someone explain me, why when I press plus and then start, timer increase the the minutes? For example if I set minutes eqaul to 10, timer starts to count from 11. When I press minus the same problem occurs. I am trying to do pomodoro clock, but first I have to fix this problem :P Thanks for any help!
Here is my code
var flag = false;
var timer;
var work = 2;
var count = 0;
$('.mins').html(updateNum(work));
$('.secs').html(updateNum(count));
function workTimer() {
if(count < 0) {
work--;
count = 59;
}
if (work<0) {
return clearInterval(timer);
}
$('.mins').html(updateNum(work));
$('.secs').html(updateNum(count));
count--;
}
$('.start-button').click(function() {
if (flag == false) {
timer=setInterval(workTimer,1000);
$(this).html("stop");
flag = true;
return flag;
}
else if(flag == true){
clearInterval(timer);
$(this).html("start");
flag = false;
return flag;
}
})
$('.session-plus').click(function() {
if(flag == false) {
console.log(work);
$('.mins').html(updateNum(work++));
}
})
$('.session-minus').click(function() {
if(flag == false) {
$('.mins').html(updateNum(work--));
}
})
function updateNum(num) {
if(num < 10) {
return num = "0" + num;
}
else {
return num;
}
}
https://jsfiddle.net/wved99o3/
#edit I do not want to do another topic, so I have next problem. My timer doesn't count from 0 as you can see. Timer adds every time 1 extra second. I have no idea why :/
https://jsfiddle.net/6vnkgt3h/
Upvotes: 0
Views: 49
Reputation: 24965
$('.mins').html(updateNum(work++));
You need to swap your ++ to preceed the variable.
$('.mins').html(updateNum(++work));
With the ++ being after the variable, the old value will be passed in, not the new value. By putting the ++ in front, it performs the increment before passing in the value.
Upvotes: 2