useCountdown
Reactive countdown timer with pause/resume/reset controls
A reactive countdown timer built on useIntervalFn. Decrements a counter by 1 on each tick, auto-pauses at 0, and provides full Pausable controls plus reset, stop, and start.
Countdown
Paused
60-second countdown with pause, resume, reset, stop, and start controls.
60
import { import useCountdown
useCountdown } from "@usels/web";
function function Component(): JSX.Element
Component() { const { const remaining$: any
remaining$ } = import useCountdown
useCountdown(60); return <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>{const remaining$: any
remaining$.any
get()}</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>; // remaining$.get() → 60, 59, 58, ... 0}import { createCountdown } from "@usels/web";
function Component() { "use scope" const { remaining$ } = createCountdown(60); return <div>{remaining$.get()}</div>; // remaining$.get() → 60, 59, 58, ... 0}With callbacks
Section titled “With callbacks”import { useCountdown } from "@usels/web";
function Component() { const { remaining$ } = useCountdown(10, { onTick: (remaining) => console.log(`${remaining}s left`), onComplete: () => console.log("Done!"), });}import { createCountdown } from "@usels/web";
function Component() { "use scope" const { remaining$ } = createCountdown(10, { onTick: (remaining) => console.log(`${remaining}s left`), onComplete: () => console.log("Done!"), });}Custom interval
Section titled “Custom interval”import { useCountdown } from "@usels/web";
function Component() { // Tick every 500ms instead of the default 1000ms const { remaining$ } = useCountdown(30, { interval: 500 });}import { createCountdown } from "@usels/web";
function Component() { "use scope" // Tick every 500ms instead of the default 1000ms const { remaining$ } = createCountdown(30, { interval: 500 });}Manual start
Section titled “Manual start”import { useCountdown } from "@usels/web";
function Component() { const { remaining$, start } = useCountdown(10, { immediate: false }); // remaining$.get() === 10 (not started) // start() to begin countdown}import { createCountdown } from "@usels/web";
function Component() { "use scope" const { remaining$, start } = createCountdown(10, { immediate: false }); // remaining$.get() === 10 (not started) // start() to begin countdown}Reset / Stop / Start controls
Section titled “Reset / Stop / Start controls”import { useCountdown } from "@usels/web";
function Component() { const { remaining$, reset, stop, start, pause, resume } = useCountdown(60);
// reset() → remaining$ back to 60 (timer keeps running) // reset(30) → remaining$ set to 30 // stop() → pause + reset (fully stop) // start() → reset + resume (restart) // start(30) → reset to 30 + resume // pause() → freeze countdown // resume() → continue from current remaining$}import { createCountdown } from "@usels/web";
function Component() { "use scope" const { remaining$, reset, stop, start, pause, resume } = createCountdown(60);
// reset() → remaining$ back to 60 (timer keeps running) // reset(30) → remaining$ set to 30 // stop() → pause + reset (fully stop) // start() → reset + resume (restart) // start(30) → reset to 30 + resume // pause() → freeze countdown // resume() → continue from current remaining$}Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
initialCount | MaybeObservable<number> | - |
options | CountdownOptions (optional) | - |
CountdownOptions
Section titled “CountdownOptions”Returns
Section titled “Returns”CountdownReturn
| Name | Type | Description |
|---|---|---|
remaining$ | ObservablePrimitive<number> | Current remaining count |
reset | (count?: number | undefined) => void | Reset to initial count or custom value |
stop | Fn | Pause + reset (stop the countdown entirely) |
start | (count?: number | undefined) => void | Reset to initial/custom count + resume (restart) |
isActive$ | ReadonlyObservable<boolean> | - |
pause | Fn | - |
resume | Fn | - |