Vue 3 composition API hooks for countdown timers, stopwatches, and live clock state.
| Hook |
Purpose |
useTimer |
Countdown to an expiry timestamp (ms since epoch) |
useStopwatch |
Count up from an optional offset |
useTime |
Current local time, 24h or 12h format |
Live demo
bun add vue-timer-hook
# or
pnpm add vue-timer-hook
# or
yarn add vue-timer-hook
# or
npm install vue-timer-hook
Peer dependency: vue ^3.2.0.
Counts down to expiryTimestamp (a number: milliseconds since Unix epoch, same as Date.now()).
<script setup lang="ts">
import { watchEffect } from 'vue'
import { useTimer } from 'vue-timer-hook'
const expiryTimestamp = Date.now() + 10 * 60 * 1000
const {
days,
hours,
minutes,
seconds,
isRunning,
isExpired,
pause,
resume,
restart,
} = useTimer(expiryTimestamp)
function restartFive() {
restart(Date.now() + 5 * 60 * 1000)
}
watchEffect(() => {
if (isExpired.value) {
console.warn('Timer expired')
}
})
</script>
<template>
<div>
<p>{{ days }}:{{ hours }}:{{ minutes }}:{{ seconds }}</p>
<p>{{ isRunning ? 'Running' : 'Paused' }}</p>
<button type="button" @click="pause()">
Pause
</button>
<button type="button" @click="resume()">
Resume
</button>
<button type="button" @click="restartFive()">
Restart 5 min
</button>
</div>
</template>
| Argument |
Type |
Default |
Description |
expiryTimestamp |
number or Ref<number> |
60 (invalid default; pass a real timestamp) |
When the countdown reaches zero |
autoStart |
boolean or Ref<boolean> |
true |
Start ticking immediately |
| Property |
Type |
Description |
seconds, minutes, hours, days |
Ref<number> |
Remaining time parts |
isRunning |
Ref<boolean> |
Timer is actively ticking |
isExpired |
Ref<boolean> |
Countdown finished |
start() |
() => void |
Start or continue from expiry |
pause() |
() => void |
Pause |
resume() |
() => void |
Resume from remaining time after pause |
restart(expiry?, autoStart?) |
(number | Ref<number>, boolean?) => void |
Reset expiry and optionally auto-start |
Counts up from an optional offset in seconds.
<script setup lang="ts">
import { useStopwatch } from 'vue-timer-hook'
const { hours, minutes, seconds, start, pause, reset } = useStopwatch(0, true)
</script>
<template>
<div>
<p>{{ hours }}:{{ minutes }}:{{ seconds }}</p>
<button type="button" @click="pause()">
Pause
</button>
<button type="button" @click="start()">
Start
</button>
<button type="button" @click="reset()">
Reset
</button>
</div>
</template>
| Argument |
Type |
Default |
Description |
offsetTimestamp |
number or Ref<number> |
60 |
Initial elapsed seconds |
autoStart |
boolean or Ref<boolean> |
true |
Start immediately |
| Property |
Type |
Description |
seconds, minutes, hours, days |
Ref<number> |
Elapsed time parts |
isRunning |
Ref<boolean> |
Stopwatch is ticking |
start() |
() => void |
Start or resume |
pause() |
() => void |
Pause |
reset(offset?, autoStart?) |
(number | Ref<number>, boolean?) => void |
Reset offset; default auto-start true |
Live clock updated every second.
<script setup lang="ts">
import { useTime } from 'vue-timer-hook'
const { hours, minutes, seconds, ampm } = useTime('12-hour')
</script>
<template>
<p>{{ hours }}:{{ minutes }}:{{ seconds }}{{ ampm }}</p>
</template>
| Argument |
Type |
Default |
Description |
format |
'12-hour' | '24-hour' or Ref<...> |
'24-hour' |
Display format |
| Property |
Type |
Description |
seconds, minutes, hours |
Ref<number> |
Current time parts |
ampm |
Ref<string> |
'am' / 'pm' when using 12-hour format, else '' |
bun install
bun run dev # demo site
bun run build # library dist (ESM + CJS + IIFE)
bun run build:dts # roll up .d.ts
bun run verify:build # build + types + dist smoke tests
bun run test # typecheck + unit tests
Inspired by react-timer-hook.