mini-timer
v1.0.2
Published
The tiniest timer to tick!
Maintainers
Readme
mini-timer
mini-timer is the smallest, minimal-dependency event driven timer that quickly and comfortably counts time for you.
pnpm i mini-timerUsage
The timer requires 2 (optional 3) arguments that define the start and end point with the increment between steps. The increment define also how long each time iteration is to count. The sign of the increment defines if the timer counter up (+) or down (-).
import timer, { type Timer } from 'mini-timer'
// From 0, count every 100ms until 10,000ms
const t = timer(0, 100, 10000)Events
The timer has multiple lifecycle events that can be listened on:
import timer, { type Timer, formatTime } from 'mini-timer'
const t = timer(10_000, -150, 0)
t.on('update', (elapsed) => formatTime(time)) // formats the time on each tick to a format of xx:xx or xx:xx:xx
t.on('start', () => console.log("Hello"))
t.on('pause', () => console.log("Pause"))
t.on('finish', (elapsed) => console.log("Preemptively finished at", elapsed))
t.start()
// Do stuff for a long time
t.pause()
// Again...
t.resume()
// The timer will stop when it reaches 0, but we can stop it preemptively
t.stop() // The default event of stop is "finish".
// Now, clean everything up with "off"
t.off('update', ...)© Torathion 2025
