fn

useCountdown

v0.0.14testeddemo

Reactive countdown timer exposing the remaining seconds plus start/stop/pause/resume/reset controls and onTick/onComplete callbacks. Built on useIntervalFn, so it is SSR-safe and cleans up on scope dispose.

Examples

ts
const { remaining, start, pause, resume, stop } = useCountdown(60);
start();
ts
useCountdown(10, {
  immediate: true,
  onTick: (n) => console.log(n),
  onComplete: () => console.log('done'),
});

Demo

Loading demo…

Signature

ts
export function useCountdown(
  initialCountdown: MaybeRefOrGetter<number>,
  options: UseCountdownOptions ={ ... }

Parameters

ParameterTypeDescription
initialCountdownMaybeRefOrGetter<number>The starting value, in seconds (can be reactive)
options?UseCountdownOptionsOptions

Returns

UseCountdownReturnThe reactive remaining value and countdown controls
PropertyTypeDescription
remainingReadonly<ShallowRef<number>>The remaining seconds, read-only (use reset/start to change it)
isActiveUseIntervalFnReturn['isActive']Whether the countdown is currently running
reset(countdown?: MaybeRefOrGetter<number>) => voidReset remaining (defaults to the initial value) without changing the running state
stop() => voidPause the countdown and reset remaining to the initial value
start(countdown?: MaybeRefOrGetter<number>) => voidReset remaining (defaults to the initial value) and start counting down