Skip to content
Timer

useIntervalFn

Reactive setInterval wrapper with pause/resume control

Execute a function repeatedly at a given interval with reactive isActive state and pause/resume control. Returns a PausableisActive is a ReadonlyObservable<boolean>.

Interval Function
Paused

Cycles through greetings every 500ms.

Hello
import {
import useIntervalFn
useIntervalFn
} from "@usels/web";
function
function Component(): void
Component
() {
const {
const isActive: any
isActive
,
const pause: any
pause
,
const resume: any
resume
} =
import useIntervalFn
useIntervalFn
(() => {
any
console
.
any
log
("tick");
}, 1000);
// isActive.get() === true while running
// pause() / resume() to control execution
}
import { useIntervalFn } from "@usels/web";
function Component() {
useIntervalFn(() => fetchData(), 5000, { immediateCallback: true });
// calls fetchData() immediately on resume, then every 5s
}
import { observable, useIntervalFn } from "@usels/web";
function Component() {
const ms$ = observable(1000);
useIntervalFn(() => {}, ms$);
// ms$.set(500) → automatically restarts with new interval
}

interval must be a positive number. 0 or negative values are passed directly to setInterval, which the browser clamps to ~4ms. Use pause() to stop execution instead of setting interval to 0.

ParameterTypeDescription
cbAnyFn-
intervalMaybeObservable<number> | undefined (optional)-
optionsIntervalFnOptions | undefined (optional)-
OptionTypeDefaultDescription
immediatebooleantrueIf true, calls resume() immediately.
immediateCallbackbooleanfalseIf true, calls fn immediately when resume() is called.

Pausable

NameTypeDescription
isActive$ReadonlyObservable<boolean>-
pauseFn-
resumeFn-

View on GitHub