@alanscodelog/utils
    Preparing search index...

    Function createIdleCallback

    • Easier to control requestIdleCallbacks with fallback support for environments without requestIdleCallback (either for browsers which don't have support or for node).

      Returns { trigger, cancelPendingCallbacks, callImmediately, isPending }.

      // e.g. a sync function, supports experimental deadline if needed
      function someHeavyFunc(deadline: IdleDeadline) {
      while (deadline.timeRemaining() > 0 && tasks.length > 0) {
      process(tasks.shift());
      }
      }

      const {
      trigger,
      callImmediately,
      cancelPendingCallbacks,
      isPending
      } = createIdleCallback(
      someHeavyFunc,
      { timeout: 2000 },
      // if you need to update a reactive variable in your framework, e.g. vue
      (_) => isPending.value = _
      )

      // above by itself does nothing, you must call trigger to queue the request
      trigger() // cancels any pending and requests idle callback

      Parameters

      • callback: IdleCallback
      • OptionalrequestIdleCallbackOptions: { timeout?: number }
      • OptionalonIsPending: (value: boolean) => void

      Returns {
          callImmediately: () => void;
          cancelPendingCallbacks: () => void;
          getIsPending: () => boolean;
          trigger: () => void;
      }