Full answer
A simplified model of one turn of the loop in a window:
- Pick a task from a task queue, for example a timer, a click, a message or a network callback. Run it.
- Run all microtasks until the microtask queue is empty. This includes promise callbacks and
queueMicrotask. - If it is time to update the display, run the rendering steps:
requestAnimationFramecallbacks, then style and layout, then paint. This is usually once per display refresh, not after every task. The browser can skip a rendering step when nothing needs to change or when the page is hidden. - Go back to step 1.
The results follow from the model:
- A DOM change made in a task is not visible until the next rendering step. Ten changes in one task cost one paint.
- A long task delays the rendering step, so the page seems frozen. Input events wait in the queue too, which is bad for INP.
- Microtasks run before rendering. A promise chain that keeps adding itself never lets the loop reach step 3, and timers never run.
requestAnimationFramecallbacks run just before paint, so DOM writes inside them are painted in that same frame. They do not run in background tabs.setTimeoutis not tied to frames, so a timer-based animation can run twice in one frame or skip a frame.
The snippet checks two claims. First, a timer task that blocks for 200 milliseconds delays the next animation frame callback until it ends. Second, 20,000 chained promise callbacks all run before a zero delay timer.
For a smooth page, keep each task under about 50 milliseconds, put visual updates in requestAnimationFrame, and avoid microtask loops that depend on external data.
<script>
window.log = [];
// 1. A blocking timer task delays the next animation frame callback.
setTimeout(() => {
const end = performance.now() + 200;
while (performance.now() < end) {}
log.push({ name: 'timer done', t: performance.now() });
}, 0);
requestAnimationFrame(() => log.push({ name: 'frame', t: performance.now() }));
// 2. Chained promises all run before a zero delay timer.
let n = 0;
const chain = () => { if (++n < 20000) Promise.resolve().then(chain); };
chain();
setTimeout(() => log.push({ name: 'timer after chain', n }), 0);
</script>Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The timer task finished before the animation frame callback, and all 20,000 chained promise callbacks ran before the zero delay timer. The order of the frame callback against the second timer can differ between runs, and the test does not depend on it.
Key points
- A turn is: one task, all microtasks, then possibly a rendering step.
- requestAnimationFrame callbacks run just before style, layout and paint.
- A long task or an endless microtask chain delays rendering and input.
- DOM changes become visible at the next rendering step, not at once.
- setTimeout is not aligned to frames.
Common mistakes
- Expecting a DOM change to appear before a long synchronous loop ends.
- Using setInterval for animation and expecting one call per frame.
- Putting heavy work in requestAnimationFrame, which makes every frame slow.
- Believing promises make code run in parallel.
Follow-ups
- Why does a hidden tab pause requestAnimationFrame but not timers?
- How would you force the browser to paint between two steps of work?
- How does this explain a spinner that stops during a heavy computation?
Tips
- Describe one turn of the loop in four steps before answering anything else.
- Connect each statement to a user-visible effect: frozen UI, delayed click.