A callback function that should be called in the near future, when the event loop is idle. The callback function is passed an IdleDeadline object describing the amount of time available and whether or not the callback has been run because the timeout period expired.
Optionaloptions: IdleRequestOptionsContains optional configuration parameters. Currently only one property is defined:
timeout If the number of milliseconds represented by this parameter has elapsed and the callback
has not already been called, then a task to execute the callback is queued in the event loop (even
if doing so risks causing a negative performance impact). timeout must be a positive value or it
is ignored.A handle which can be used to cancel the callback by passing it into the cancelIdleCallback() method.
// Obtain the native function via the helper (returns null when unavailable)
const reqIdle: RequestIdleCallback | null = getIdleCallback();
if (reqIdle) {
const handle = reqIdle((deadline) => {
while (deadline.timeRemaining() > 0) {
// perform low-priority background work
}
}, { timeout: 2000 });
// cancel later if needed
const cancelIdle = getCancelIdleCallback();
cancelIdle && cancelIdle(handle);
}
Type alias for the global
requestIdleCallbackfunction, which schedules a callback to be invoked during the browser's idle periods. Using a type alias makes it easier to reference and annotate the function, and supports older TypeScript versions that may not include the global declaration.Prefer using scheduleIdleCallback over calling
requestIdleCallbackdirectly, as it provides a cross-environment fallback and returns a cancellable ITimerHandler. Use this type (via getIdleCallback) only when you need the raw browser API.