Skip to content
Timer

useTimeout

Reactive boolean that becomes true after a given delay

A thin wrapper around useTimeoutFn that exposes a ReadonlyObservable<boolean> (ready$) that flips to true when the timeout completes.

Timeout
Waiting

Ready becomes true after 1 second.

import {
import useTimeout
useTimeout
} from "@usels/web";
function
function Component(): JSX.Element
Component
() {
const
const ready$: any
ready$
=
import useTimeout
useTimeout
(1000);
return <
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>{
const ready$: any
ready$
.
any
get
() ? "Ready!" : "Waiting..."}</
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>;
// ready$.get() === false while waiting
// ready$.get() === true after 1000ms
}
import { useTimeout } from "@usels/web";
function Component() {
useTimeout(500, {
callback: () => console.log("done!"),
});
}
import { useTimeout } from "@usels/web";
function Component() {
const { ready$, isPending$, stop, start } = useTimeout(1000, { controls: true });
// stop() → cancel the pending timeout
// start() → restart the timer
}
import { useTimeout } from "@usels/web";
function Component() {
const { ready$, start } = useTimeout(1000, {
controls: true,
immediate: false,
});
// call start() when you're ready
}
ObservableMeaning
ready$true when timeout has fired
isPending$ (controls only)true while timeout is still waiting

They are inverses: ready$ = !isPending$.

Pass an Observable<number> as the first argument and useTimeoutFn will read the latest value each time start() is called.

ParameterTypeDescription
intervalMaybeObservable<number>-
optionsTimeoutOptions & { controls: true; }-
OptionTypeDefaultDescription
controlsboolean--
callbackFn-Callback invoked on timeout completion
immediatebooleantrueAuto-start immediately.

TimeoutReturn & Stoppable<any[]>

NameTypeDescription
ready$ObservableBoolean<boolean>-
isPending$ObservableBoolean<boolean>-
stopFn-
start(...args: any[]) => void-

View on GitHub