Skip to content

useTimeoutFn

Execute a function after a given delay with reactive isPending$ state and manual start/stop control. Returns a StoppableisPending$ is an Observable<boolean> you can subscribe to.

Please wait for 3 seconds
import {
import useTimeoutFn
useTimeoutFn
} from "@usels/web";
const {
const isPending$: any
isPending$
,
const start: any
start
,
const stop: any
stop
} =
import useTimeoutFn
useTimeoutFn
(() => {
any
console
.
any
log
("fired!");
}, 1000);
// isPending$.get() === true while waiting
// stop() cancels the pending timeout
// start() restarts the timer (resets if already pending)
import {
import useTimeoutFn
useTimeoutFn
} from "@usels/web";
const {
const isPending$: any
isPending$
,
const start: any
start
} =
import useTimeoutFn
useTimeoutFn
(() =>
any
console
.
any
log
("done"), 500, {
immediate: boolean
immediate
: false });
// call start() manually when ready
const start: any
start
();
import {
import useTimeoutFn
useTimeoutFn
} from "@usels/web";
import {
function observable<T>(): Observable<T | undefined> (+2 overloads)
observable
} from "@legendapp/state";
const
const delay$: any
delay$
=
observable<unknown>(value: Promise<unknown> | (() => unknown) | unknown): any (+2 overloads)
observable
(1000);
const {
const start: any
start
} =
import useTimeoutFn
useTimeoutFn
(() => {},
const delay$: any
delay$
);
// start() always reads current value of delay$
import {
import useTimeoutFn
useTimeoutFn
} from "@usels/web";
const {
const start: any
start
} =
import useTimeoutFn
useTimeoutFn
((
msg: string | undefined
msg
?: string) =>
any
console
.
any
log
("fired",
msg: string | undefined
msg
), 1000, {
immediate: boolean
immediate
: false,
immediateCallback: boolean
immediateCallback
: true,
});
const start: any
start
("hello");
// → cb() called immediately with no args
// → cb("hello") called again after 1000ms
export interface UseTimeoutFnOptions {
immediate?: boolean;
immediateCallback?: boolean;
}
export declare function useTimeoutFn<CallbackFn extends AnyFn>(cb: CallbackFn, interval: MaybeObservable<number>, options?: UseTimeoutFnOptions): Stoppable<Parameters<CallbackFn> | []>;

View on GitHub

  • tigerwest
  • a7392ab 2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)