Skip to main content

useTimeout

Update value after a given time

Usage

Live Editor

function 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>
  );
};

Result
Loading...

API

useTimeout

Returns

Stoppable: A tuple with the following elements:

  • Whether to wait for the timer to execute.
  • Set timer.
  • Cancel timer.

Arguments

ArgumentDescriptionTypeDefaultValue
mswait timenumber | undefined-
options-UseTimeoutOptions | undefined-

UseTimeoutOptions

PropertyDescriptionTypeDefaultValue
immediateStart the timer immediate after calling this functionbooleanfalse