Recursive setTimeout

suggest change

To 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents