Timer Functions in JavaScript

From the Async JavaScript cheat sheet · Timers & Event Loop · verified Jul 2026

Timer Functions

Schedule code execution after delays or at regular intervals using built-in timer functions

javascript
// setTimeout
const timeoutId = setTimeout(() => {
  console.log('Executed after 2 seconds');
}, 2000);

// setInterval
const intervalId = setInterval(() => {
  console.log('Every second');
}, 1000);

// Clear timers
clearTimeout(timeoutId);
clearInterval(intervalId);

// setImmediate (Node.js)
setImmediate(() => {
  console.log('Execute after I/O');
});
💡 setTimeout executes once after a delay, setInterval repeats
⚠️ Timers are not guaranteed to execute at exact times
🔄 Always clear intervals and timeouts when no longer needed
⚡ Use setTimeout with 0ms to defer execution to next tick

More JavaScript tasks

Back to the full Async JavaScript cheat sheet