@nevware21/ts-utils
    Preparing search index...

    Type Alias RequestIdleCallback

    RequestIdleCallback: (
        callback: IdleRequestCallback,
        options?: IdleRequestOptions,
    ) => number

    Type alias for the global requestIdleCallback function, 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 requestIdleCallback directly, 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.

    Type Declaration

      • (callback: IdleRequestCallback, options?: IdleRequestOptions): number
      • Parameters

        • callback: IdleRequestCallback

          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: IdleRequestOptions

          Contains 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.

        Returns number

        A handle which can be used to cancel the callback by passing it into the cancelIdleCallback() method.

    0.11.2

    // 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);
    }
    // Annotate a helper that wraps requestIdleCallback with a typed variable
    let _reqIdle: RequestIdleCallback | null = null;

    function getReqIdle(): RequestIdleCallback | null {
    return _reqIdle || (_reqIdle = getIdleCallback());
    }