I created a Pomodoro countdown timer using JavaScript and jQuery. The code snippet I'm talking about is the following:
var time = 1500;
var cycle = "long";
var tracker = 0;
var paused = false;
function countdown(){
if (!paused) {
var seconds = ("00" + (time % 60)).slice(-2);
$("#time").text(Math.floor(time/60) + ":" + seconds);
$("title").text(Math.floor(time/60) + ":" + seconds);
if (time > 0){
time--;
setTimeout(countdown, 1000);
} else {
document.getElementById("bell").play();
tracker++;
if (tracker == 7) {
cycle = "long";
time = 1500;
setTimeout(countdown, 1000);
} else if (tracker == 8) {
time = 1500;
setTimeout(countdown, 1000);
tracker = 0;
} else if (cycle == "short" && tracker < 7) {
cycle = "long";
time = 1500;
setTimeout(countdown, 1000);
} else if (cycle == "long" && tracker < 7) {
cycle = "short";
time = 300;
setTimeout(countdown, 1000);
}
}
} else {
setTimeout(countdown, 1);
}
}
setTimeout(countdown(), 1000);
The function calls "setTimeout (countdown, 1000);" located inside the "else" parts of if statements causes a break in the program when it is written as follows: "setTimeout ( countdown () , 1000);" because, as I believe, the function should be called, I am completely puzzled by this and very I appreciate if someone was kind enough to give an explanation.
Question: Why is the program interrupted if "()" is added after the word "countdown"?
source