Recursive setTimeout
suggest changeTo repeat a function indefinitely, setTimeout can be called recursively:
function repeatingFunc() {
console.log("It's been 5 seconds. Execute the function again.");
setTimeout(repeatingFunc, 5000);
}
setTimeout(repeatingFunc, 5000);
The next call is scheduled after this one returns, so the callback does not overlap itself. The delay is a minimum, and background tabs may clamp it. If the function throws before the recursive setTimeout, the chain stops. setInterval keeps scheduling even when the callback throws, and it does not wait for the previous callback to finish.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents