Timer System
@esengine/timer provides a flexible timer and cooldown system for delayed execution, repeating tasks, skill cooldowns, and more.
Installation
Section titled “Installation”npm install @esengine/timerQuick Start
Section titled “Quick Start”import { createTimerService } from '@esengine/timer';
// Create timer serviceconst timerService = createTimerService();
// One-time timer (executes after 1 second)const handle = timerService.schedule('myTimer', 1000, () => { console.log('Timer fired!');});
// Repeating timer (every 100ms)timerService.scheduleRepeating('heartbeat', 100, () => { console.log('Tick');});
// Cooldown system (5 second cooldown)timerService.startCooldown('skill_fireball', 5000);
if (timerService.isCooldownReady('skill_fireball')) { useFireball(); timerService.startCooldown('skill_fireball', 5000);}
// Update in game loopfunction gameLoop(deltaTime: number) { timerService.update(deltaTime);}Core Concepts
Section titled “Core Concepts”Timer vs Cooldown
Section titled “Timer vs Cooldown”| Feature | Timer | Cooldown |
|---|---|---|
| Purpose | Delayed code execution | Rate limiting |
| Callback | Has callback function | No callback |
| Repeat | Supports repeating | One-time |
| Query | Query remaining time | Query progress/ready status |
TimerHandle
Section titled “TimerHandle”Handle object returned when scheduling a timer:
interface TimerHandle { readonly id: string; // Timer ID readonly isValid: boolean; // Whether valid (not cancelled) cancel(): void; // Cancel timer}TimerInfo
Section titled “TimerInfo”interface TimerInfo { readonly id: string; // Timer ID readonly remaining: number; // Remaining time (ms) readonly repeating: boolean; // Whether repeating readonly interval?: number; // Interval (repeating only)}CooldownInfo
Section titled “CooldownInfo”interface CooldownInfo { readonly id: string; // Cooldown ID readonly duration: number; // Total duration (ms) readonly remaining: number; // Remaining time (ms) readonly progress: number; // Progress (0-1, 0=started, 1=finished) readonly isReady: boolean; // Whether ready}Documentation
Section titled “Documentation”- API Reference - Complete timer and cooldown API
- Examples - Skill cooldowns, DOT effects, buff systems
- Best Practices - Usage tips and ECS integration