Skip to content

useNumberCountdown

Counts down a duration given in milliseconds. Unlike useDateCountdown, the remaining time is held while paused: resuming continues from where it stopped. Internally the remaining time is recomputed from timestamps on every tick, so it does not accumulate interval drift while running.

Demo

0
Years
0
Months
0
Days
0
Hours
0
Minutes
0
Seconds
0
Milliseconds

Usage

vue
<script setup>
import { useNumberCountdown } from "vue-countdown-composable";

const { minutes, seconds, isRunning, start, pause, resume, stop } =
  useNumberCountdown({
    onFinish: () => console.log("Time is up! ⏰"),
  });

// count down 5 minutes, updating every second
start(5 * 60 * 1000);
</script>

<template>
  <div>{{ minutes }}m {{ seconds }}s</div>
</template>

API

Options

useNumberCountdown(options?) accepts an optional options object:

NameTypeDescription
onFinish() => voidCalled once when a started countdown reaches zero. Not called on stop() or when start receives an invalid duration.

Returned values

All values are read-only and reactive.

NameTypeDescription
millisecondsComputedRef<number>Remaining milliseconds (0–999)
secondsComputedRef<number>Remaining seconds (0–59)
minutesComputedRef<number>Remaining minutes (0–59)
hoursComputedRef<number>Remaining hours (0–23)
daysComputedRef<number>Remaining days (0–29)
monthsComputedRef<number>Remaining months (0–11), approximated as 30 days
yearsComputedRef<number>Remaining years, approximated as 12 × 30-day months
isRunningReadonly<Ref<boolean>>Whether the countdown is currently ticking

Months and years are approximations

A duration has no calendar context, so months are treated as 30 days and years as 12 of those months.

Methods

NameSignatureDescription
start(countdownTime: number, updateInterval?: number) => voidStart counting down from countdownTime ms, updating every updateInterval ms (default 1000; invalid values warn and fall back). Calling it again restarts. A non-positive or invalid duration results in a finished (all zeros) countdown.
pause() => voidPause the countdown, holding the remaining time.
resume() => voidResume a paused countdown from the held remaining time. No-op if already running or finished.
stop() => voidStop the countdown and reset all values to zero.

Cleanup

The interval is cleared automatically when the owning component (or effectScope) is disposed — no manual cleanup needed.

Released under the MIT License.