CodePark/ questions / Browser APIs
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › Browser APIs

What is requestIdleCallback best suited for?

Low-priority background work, like sending analytics, when the main thread is idle

The callback runs during idle periods, when the main thread has nothing else to do. It receives a deadline that tells you how much time is left. You can also pass a timeout so the callback runs eventually, even if the browser is never idle.

"Updating the DOM for a smooth animation" is the tempting wrong answer. DOM updates for animation belong in
requestAnimationFrame, which runs before each paint. Handling user clicks is urgent work, so it belongs in the event handler itself. Running code before the page unloads needs an unload or pagehide event, because idle time may never come.