After creating a loop with the setTimeout function, I encountered an issue where it would call itself after the 2nd or 3rd step because it started executing twice simultaneously. Here is how my function looks:
var value = 70,
intervalID = null;
function interval() {
intervalID = setTimeout(countDown, 1000);
}
function countDown() {
value--;
if(value > 0) {
clearTimeout(intervalID);
interval();
} else {
endInterval();
}
}
function endInterval() {
// do something
}
If I console.log the variable 'value', it shows 69 and then 68, but afterwards it starts decreasing twice in one function call. I only call the function 'countDown()' from one place.
What could be causing this issue?
Edit: The code is now functioning properly.