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 { createTimeout } from "@usels/web";
function Component() { "use scope" const ready$ = createTimeout(1000); return <div>{ready$.get() ? "Ready!" : "Waiting..."}</div>; // ready$.get() === false while waiting // ready$.get() === true after 1000ms}With callback
Section titled “With callback”import { useTimeout } from "@usels/web";
function Component() { useTimeout(500, { callback: () => console.log("done!"), });}import { createTimeout } from "@usels/web";
function Component() { "use scope" createTimeout(500, { callback: () => console.log("done!"), });}With stop/start controls
Section titled “With stop/start controls”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 { createTimeout } from "@usels/web";
function Component() { "use scope" const { ready$, isPending$, stop, start } = createTimeout(1000, { controls: true });
// stop() → cancel the pending timeout // start() → restart the timer}Manual start (immediate: false)
Section titled “Manual start (immediate: false)”import { useTimeout } from "@usels/web";
function Component() { const { ready$, start } = useTimeout(1000, { controls: true, immediate: false, });
// call start() when you're ready}import { createTimeout } from "@usels/web";
function Component() { "use scope" const { ready$, start } = createTimeout(1000, { controls: true, immediate: false, });
// call start() when you're ready}ready$ vs isPending$
Section titled “ready$ vs isPending$”| Observable | Meaning |
|---|---|
ready$ | true when timeout has fired |
isPending$ (controls only) | true while timeout is still waiting |
They are inverses: ready$ = !isPending$.
Reactive interval
Section titled “Reactive interval”Pass an Observable<number> as the first argument and useTimeoutFn will read the latest value each time start() is called.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
interval | MaybeObservable<number> | - |
options | TimeoutOptions & { controls: true; } | - |
TimeoutOptions
Section titled “TimeoutOptions”Returns
Section titled “Returns”TimeoutReturn & Stoppable<any[]>
| Name | Type | Description |
|---|---|---|
ready$ | ObservableBoolean<boolean> | - |
isPending$ | ObservableBoolean<boolean> | - |
stop | Fn | - |
start | (...args: any[]) => void | - |