katanakit-js / useInterval
Function: useInterval()
ts
function useInterval(
callback,
ms,
immediate?
): IntervalControl;Defined in: src/core/services/timing.service.ts:72
Repeatedly executes a callback at ms intervals with pause/resume/stop controls. Uses recursive setTimeout internally to avoid overlap.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
callback | () => void | Promise<void> | undefined | Function executed on each tick. |
ms | number | undefined | Interval in milliseconds. |
immediate | boolean | false | If true, fires the callback immediately before the first interval (defaults to false). |
Returns
An IntervalControl with pause, resume, stop, and isRunning.
Example
ts
const timer = useInterval(() => console.log("tick"), 1000);
setTimeout(() => timer.pause(), 3000);
setTimeout(() => timer.resume(), 5000);
setTimeout(() => timer.stop(), 10000);