Timer Manager
Delays, repeating timers and cooldowns that run on game time, so they stop when the game is paused and speed up or slow down with it (unlike setTimeout).
Files: timer-manager.js (the component, no dependencies) and timer-manager.test.mjs (5 tests).
Copy timer-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createTimers } from "./timer-manager.js";
const timers = createTimers();
timers.after(2, () => spawnBoss());
timers.every(0.5, () => spawnEnemy(), { times: 10, tag: "wave" });
const dash = timers.cooldown(1.2);
if (input.pressed("dash") && dash.ready()) { dash.trigger(); player.dash(); }
drawCooldownRing(dash.progress); // 0 just used .. 1 ready
function frame(dt) { if (!paused) timers.update(dt); }
timers.timeScale = 0.3; // slow motion
API
createTimers() → timerstimers.after(seconds, fn, { tag }) → handletimers.every(seconds, fn, { times, tag, now }) → handletimes = how many runs (default forever), now = run once straight awayhandle.cancel() / handle.pause() / handle.resume() / handle.remaining / handle.donetimers.cooldown(seconds) → { ready(), trigger() → true if it was ready, reset(), remaining, progress }timers.update(dt)a long frame runs a repeating timer as many times as it should havetimers.timeScale1 normal, 0.5 half speed, 0 frozentimers.cancelTag(tag) / timers.pauseTag(tag) / timers.resumeTag(tag) / timers.clear() / timers.counttimers.timetotal game time in seconds
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes. Phaser has this.time.delayedCall; this one is engine-free and has cooldowns. |
Tests
Node tests: after, every with times and now, catching up after a long frame, cancel inside a callback, pause and resume, tags, time scale, cooldowns.
Run them with node --test toolkit/components/timer-manager/ (Node 20 or newer, nothing to install).