Idle callbacks (requestIdleCallback)

A browser callback for optional work that can wait until the main thread has a quiet moment, without competing with the current screen.

run code when the browser has nothing else to dodo background work after the page settleswait until the main thread is freedo this whenever, it is not importantrequest idle callbackrequest idle calbackwarm up the next page in the backgrounddo this later without blocking clicks

See it

Live demo coming soon

What it is

'requestIdleCallback' asks the browser to run non-urgent JavaScript during an idle gap between higher-priority work. Its callback receives an 'IdleDeadline'; 'timeRemaining()' estimates how much of the current gap is left, and 'didTimeout' tells you when an optional deadline forced the callback to run.

Use it to warm a cache, precompute optional data, or process a queue that does not affect the current screen. Break the work into small units, stop when 'timeRemaining()' runs low, and schedule the remaining units in another idle callback.

Gotcha: an idle callback may be delayed for a long time on a busy page, while a 'timeout' can make it run during that busy period. Never put a required save, visible update, or correctness-critical cleanup behind it. Provide a scheduling fallback where the API is unavailable and keep each unit small even when 'didTimeout' is true.

Ask AI for it

Move this non-urgent queue behind 'window.requestIdleCallback'. Process one small item at a time while 'deadline.timeRemaining() > 5', then schedule another callback for the remaining items. Set a bounded 'timeout' only if eventual completion matters, and still limit the amount processed when 'deadline.didTimeout' is true. Add a fallback using 'setTimeout' for browsers without 'requestIdleCallback', or prefer 'scheduler.postTask' with priority 'background' where the Prioritized Task Scheduling API is available, since it gives the same low priority with a cancellable TaskSignal. Keep visible UI updates, data persistence, and required cleanup out of the idle queue, and cancel the pending callback with 'cancelIdleCallback' when its owning component unmounts.

You might have meant

main thread blockinglong taskinteraction to next paintthird party script taxtotal blocking time

Go deeper