Timer
@ailura/alpinejs-timer
A $timer magic for countdowns, countups, and stopwatches. The plugin owns the
drift: it schedules against timestamps rather than counting ticks, so a background tab
does not make a 30 second timer jump.
Install
Section titled “Install”pnpm add alpinejs @ailura/alpinejs-timerRegister the plugin
Section titled “Register the plugin”Do this once, before Alpine.start().
import Alpine from "alpinejs";import timerPlugin from "@ailura/alpinejs-timer";
Alpine.plugin(timerPlugin());
Alpine.start();That registers the $timer magic. There is no store.
Minimal example
Section titled “Minimal example”A 30 second countdown that starts on click.
<div x-data="{ tm: $timer.countdown({ duration: 30000 }) }"> <p><span x-text="tm.formatted"></span> remaining</p> <p>Progress: <span x-text="Math.round(tm.progress * 100) + '%'"></span></p>
<button @click="tm.start()">Start</button> <button @click="tm.pause()">Pause</button> <button @click="tm.restart()">Restart</button></div>Each factory takes an options object and returns a fresh timer. Build it once in
x-data and drive it through tm, because a timer built inside a binding expression
would be recreated on every render.
Choosing a kind
Section titled “Choosing a kind”countdown({ duration })— counts down to zero from a duration.durationis required.countup({ limit })— counts up toward a limit, useful for a progress bar.stopwatch()— counts up with no target.create({ direction })— the general factory,'up'or'down'.
$timer.countdown({ duration: 30000 });$timer.countup({ limit: 30000 });$timer.stopwatch();Variants
Section titled “Variants”Toggle and reset from one control. toggle() flips between running and paused,
reset() goes back to zero and stops, and restart() goes back and begins again. These
live on the countdown, countup and create() views.
tm.toggle();tm.reset();tm.restart();Watch completion. Pass onComplete (or onTick) and the timer calls you when it
reaches the end, so you do not have to poll remaining.
$timer.countdown({ duration: 30000, onComplete: () => celebrate() });Record laps on a stopwatch. createStopwatch() also tracks laps, with the fastest
and slowest kept for you.
import { createStopwatch } from "@ailura/alpinejs-timer";
const sw = createStopwatch();sw.start();sw.lap();sw.laps; // every recorded lapsw.lastLap; // the most recentsw.fastestLap; // the fastest so farAPI reference
Section titled “API reference”Every method below is on the object $timer.countdown(), $timer.countup() and
$timer.create() return, named tm in the examples.
| Name | Type | Purpose |
|---|---|---|
tm.formatted |
store | The time as a display string. |
tm.remaining |
store | Milliseconds left, or null when there is no duration. |
tm.elapsed |
store | Milliseconds elapsed. |
tm.duration |
store | The target in milliseconds. |
tm.progress |
store | From 0 to 1, for a progress bar. |
tm.running |
store | Whether it is ticking. |
tm.paused |
store | Whether it is paused. |
tm.completed |
store | Whether it reached the end. |
tm.direction |
store | 'up' or 'down'. |
tm.iteration |
store | How many times a repeating timer has restarted. |
tm.start() |
method | Start, or resume after a pause. |
tm.pause() |
method | Pause. |
tm.resume() |
method | Resume from a pause. |
tm.toggle() |
method | Start if paused, pause if running. |
tm.reset() |
method | Back to zero, stopped. |
tm.restart() |
method | Back to zero and start. |
tm.destroy() |
method | Tear the timer down. |
Teardown
Section titled “Teardown”A timer built in a template expression belongs to the element that expression is on.
When Alpine removes that element, the view is torn down for you: the pending timeout is
cleared and the controller is destroyed, so it can no longer tick or be re-armed with
start(). That covers the x-data case above, where the timer lives and dies with the
component it is declared on.
Plugin options
Section titled “Plugin options”timerPlugin({ magicKey: "stopwatch" });magicKey defaults to timer. The options each factory accepts are direction,
duration, limit (count-up), initialElapsed, autoStart, precision (default
16), repeat, format, onTick, and onComplete.