useTimeoutFn

Wrapper for setTimeout with controls.

Usage

import { useTimeout } from "@reactuses/core";

const Demo = () => {
  const [isPending, start, cancel] = useTimeout(5000);

  return (
    <div>
      <div>Pending: {JSON.stringify(isPending)}</div>
      <button
        onClick={() => {
          start();
        }}
      >
        Start Again
      </button>
      <button
        onClick={() => {
          cancel();
        }}
      >
        Cancel
      </button>
    </div>
  );
};

Example

Please wait for 3 seconds

Type Declarations

export interface UseTimeoutFnOptions {
  /**
   * Start the timer immediate after calling this function
   *
   * @default false
   */
  immediate?: boolean
}
/**
 * Wrapper for `setTimeout` with controls.
 *
 * @param cb
 * @param interval
 * @param options
 */
export default function useTimeoutFn(
  cb: (...args: unknown[]) => any,
  interval: number,
  options?: UseTimeoutFnOptions
): Stoppable