Skip to main content

useIdle

Invoke the callback when the browser is idle. This hook uses window.requestIdleCallback by default. If the window.requestIdleCallback is non-existent, it uses setTimeout.

API

function useIdle(callback: () => void): [start: () => void, cancel: () => void];

Params:

  • callback: A function that will be invoked.

Results:

  • start: Start function.
  • cancel: Cancel function.

Example

import React from "react";
import { useIdle } from "@lilib/hooks";

function Example() {
const [start, cancel] = useIdle(() => {
// Do something
});
}