The handle returned by requestIdleCallback that identifies the idle
callback to cancel. Passing an invalid or already-consumed handle is a no-op.
// Schedule then immediately cancel an idle callback
const reqIdle = getIdleCallback();
const cancelIdle: CancelIdleCallback | null = getCancelIdleCallback();
if (reqIdle && cancelIdle) {
const handle = reqIdle((deadline) => {
// background work
});
// cancel before the callback fires
cancelIdle(handle);
}
Type alias for the global
cancelIdleCallbackfunction, which cancels a previously scheduled idle callback. 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.When using scheduleIdleCallback you can cancel the scheduled work through the returned ITimerHandler instead of calling
cancelIdleCallbackdirectly. Use this type (via getCancelIdleCallback) only when you need the raw browser API.