The Event Loop, Visualized

setTimeout(0), alone — interactive event loop example

1console.log('before');
2
3setTimeout(() => {
4 console.log('later');
5}, 0);
6
7console.log('after');

The script starts on the call stack. console.log("before") runs synchronously.

1 / 7

Call stack

script

Web APIs — browser land

The event loop — is the call stack empty? → drain the microtask queue first → then take one callback.

Microtask queue — priority

Callback queue

Console

01before

Fig. — setTimeout(0), alone · step 1 of 7

What is going on here

setTimeout(fn, 0) is the shortest possible demonstration that a delay is a minimum, not a schedule. The callback does not run in zero milliseconds, and it does not run "as soon as possible" in any intuitive sense — it runs when the call stack is empty and the loop reaches it.

What actually happens is that setTimeout hands the callback to the browser and returns immediately. The 0ms timer expires almost at once, which only means the callback is now ready: it moves into the task queue. A queued task cannot preempt running code, so it waits there while the rest of the script executes.

This is why setTimeout(fn, 0) is sometimes used to "defer until after the current work" — pushing a DOM read past a layout change, or breaking a long job into chunks so the browser can render between them. It is a real technique, but its guarantee is only "after the current task and its microtasks", never a timing promise. Nested timers are additionally clamped to about 4ms.

Common questions

Does setTimeout with 0 delay run immediately?

No. Zero specifies a minimum delay, not an immediate call. The callback becomes a task and can only run once the current script has finished and the call stack is empty.

What is setTimeout(fn, 0) actually used for?

It defers work until after the current task, which is useful for letting the browser render between chunks of a long job or for reading DOM state after pending updates. It is a scheduling hint, not a timing guarantee.

Why is the minimum setTimeout delay sometimes 4ms?

The HTML specification clamps nested timers — a timer set from inside a timer callback, beyond a nesting depth of five — to at least four milliseconds, which stops runaway timer recursion from monopolising the loop.

Every question on one page →·Run your own code →

Embed this figure in your own post →

Drop this into any article or course page. It stays interactive — readers can step through it without leaving your site.