I have created a countdown "Pomodoro" timer using JavaScript and JQuery. The code snippet I am referring to is the following:
var time = 1500;
var cycle = "long";
var tracker = 0;
var paused = false;
//Timer countdown function
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);
//Once the time is up, determine cycle, play chime and reset timer.
} 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" portions of the if statements cause a break in the program when written as follows: "setTimeout(countdown(), 1000);", as I believe a function should normally be invoked. I am completely baffled by this and would very much appreciate if someone was kind enough to offer an explanation.
Question: Why does the program break if "()" is added after the word "countdown"?