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.
See it
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.