Bash.tv → GitHub → itch.io

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

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).