🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

setTimeout()

AI & DATA SCIENCE // set-timeout

setTimeout schedules a callback to run ONCE after a minimum delay in milliseconds. Returns a timer ID for cancellation.

Syntax

const id = setTimeout(callback, delay, ...args);
clearTimeout(id); // cancel if not yet fired

Deep Dive Course

**setTimeout** is a Web API (not core JavaScript). The callback runs in the **event loop** after the call stack is empty AND after the delay. The delay is a **minimum** — if the call stack is busy, it runs later. A delay of 0ms still defers to after the current synchronous code finishes.

1Understanding setTimeout()

setTimeout is a Web API (not core JavaScript). The callback runs in the event loop after the call stack is empty AND after the delay. The delay is a minimum — if the call stack is busy, it runs later. A delay of 0ms still defers to after the current synchronous code finishes.

💡

setTimeout with 0ms delay doesn't run immediately — it defers to the next event loop iteration. This is used to break up long tasks.

editor.html
console.log('1: before');

const id = setTimeout(() => {
  console.log('3: in setTimeout (500ms later)');
}, 500);

console.log('2: after (synchronous)');

// To cancel before it fires:
clearTimeout(id); // if we changed our mind
localhost:3000

2Practical Example

Here is a real-world application of setTimeout() showing how it is used in production JavaScript code.

editor.html
// 0ms delay defers to next event loop tick
console.log('start');

setTimeout(() => console.log('deferred'), 0);

console.log('end');
// Order: start, end, deferred
localhost:3000

3Best Practices

Follow these guidelines when working with setTimeout():

1. Store the return ID for potential cancellation with clearTimeout

2. Don't rely on exact timing — delays are minimums

3. Use 0ms delay to defer code to after current execution

⚠️

Tip: setTimeout with 0ms delay doesn't run immediately — it defers to the next event loop iteration. This is used to break up long tasks.

editor.html
console.log('1: before');

const id = setTimeout(() => {
  console.log('3: in setTimeout (500ms later)');
}, 500);

console.log('2: after (synchronous)');

// To cancel before it fires:
clearTimeout(id); // if we changed our mind
localhost:3000

Examples

Example 01Basic Usage
console.log('1: before');

const id = setTimeout(() => {
  console.log('3: in setTimeout (500ms later)');
}, 500);

console.log('2: after (synchronous)');

// To cancel before it fires:
clearTimeout(id); // if we changed our mind
Example 02Advanced Example
// 0ms delay defers to next event loop tick
console.log('start');

setTimeout(() => console.log('deferred'), 0);

console.log('end');
// Order: start, end, deferred

Best Practices

  • Store the return ID for potential cancellation with clearTimeout
  • Don't rely on exact timing — delays are minimums
  • Use 0ms delay to defer code to after current execution

Interview Question

Why does setTimeout(fn, 0) not run immediately?

Hint: The event loop and call stack.

setTimeout registers the callback in the Timer Web API. After the delay (even 0ms), it moves to the callback queue. The event loop only picks callbacks from the queue when the CALL STACK IS EMPTY. Since current synchronous code is still on the stack, the 0ms callback runs only after all synchronous code completes.

Exercises

MediumPractice using setTimeout() in a real scenario.
View Solution
console.log('1: before');

const id = setTimeout(() => {
  console.log('3: in setTimeout (500ms later)');
}, 500);

console.log('2: after (synchronous)');

// To cancel before it fires:
clearTimeout(id); // if we changed our mind

Frequently Asked Questions

Why does setTimeout(fn, 0) not run immediately?

setTimeout registers the callback in the Timer Web API. After the delay (even 0ms), it moves to the callback queue. The event loop only picks callbacks from the queue when the CALL STACK IS EMPTY. Since current synchronous code is still on the stack, the 0ms callback runs only after all synchronous code completes.

Related Functions

setIntervalPromisesEvent Loopawait