Skip to content

API Reference

function createTimerService(config?: TimerServiceConfig): ITimerService

Configuration:

PropertyTypeDefaultDescription
maxTimersnumber0Maximum timer count (0 = unlimited)
maxCooldownsnumber0Maximum cooldown count (0 = unlimited)

Schedule a one-time timer:

const handle = timerService.schedule('explosion', 2000, () => {
createExplosion();
});
// Cancel early
handle.cancel();

Schedule a repeating timer:

// Execute every second
timerService.scheduleRepeating('regen', 1000, () => {
player.hp += 5;
});
// Execute immediately once, then repeat every second
timerService.scheduleRepeating('tick', 1000, () => {
console.log('Tick');
}, true); // immediate = true

Cancel timers:

// Cancel by handle
handle.cancel();
// or
timerService.cancel(handle);
// Cancel by ID
timerService.cancelById('regen');

Check if timer exists:

if (timerService.hasTimer('explosion')) {
console.log('Explosion is pending');
}

Get timer information:

const info = timerService.getTimerInfo('explosion');
if (info) {
console.log(`Remaining: ${info.remaining}ms`);
console.log(`Repeating: ${info.repeating}`);
}

Start a cooldown:

timerService.startCooldown('skill_fireball', 5000);

Check cooldown status:

if (timerService.isCooldownReady('skill_fireball')) {
castFireball();
timerService.startCooldown('skill_fireball', 5000);
} else {
console.log('Skill still on cooldown');
}
// or use isOnCooldown
if (timerService.isOnCooldown('skill_fireball')) {
console.log('On cooldown...');
}

getCooldownProgress / getCooldownRemaining

Section titled “getCooldownProgress / getCooldownRemaining”

Get cooldown progress:

// Progress 0-1 (0=started, 1=complete)
const progress = timerService.getCooldownProgress('skill_fireball');
console.log(`Progress: ${(progress * 100).toFixed(0)}%`);
// Remaining time (ms)
const remaining = timerService.getCooldownRemaining('skill_fireball');
console.log(`Remaining: ${(remaining / 1000).toFixed(1)}s`);

Get complete cooldown info:

const info = timerService.getCooldownInfo('skill_fireball');
if (info) {
console.log(`Duration: ${info.duration}ms`);
console.log(`Remaining: ${info.remaining}ms`);
console.log(`Progress: ${info.progress}`);
console.log(`Ready: ${info.isReady}`);
}

Reset cooldowns:

// Reset single cooldown
timerService.resetCooldown('skill_fireball');
// Clear all cooldowns (e.g., on respawn)
timerService.clearAllCooldowns();

Update timer service (call every frame):

function gameLoop(deltaTime: number) {
timerService.update(deltaTime); // deltaTime in ms
}

Clear all timers and cooldowns:

timerService.clear();
// Get active timer count
console.log(timerService.activeTimerCount);
// Get active cooldown count
console.log(timerService.activeCooldownCount);
// Get all active timer IDs
const timerIds = timerService.getActiveTimerIds();
// Get all active cooldown IDs
const cooldownIds = timerService.getActiveCooldownIds();
  • StartCooldown - Start cooldown
  • IsCooldownReady - Check if cooldown is ready
  • GetCooldownProgress - Get cooldown progress
  • GetCooldownInfo - Get cooldown info
  • ResetCooldown - Reset cooldown
  • HasTimer - Check if timer exists
  • CancelTimer - Cancel timer
  • GetTimerRemaining - Get timer remaining time

For dependency injection:

import { TimerServiceToken, createTimerService } from '@esengine/timer';
// Register service
services.register(TimerServiceToken, createTimerService());
// Get service
const timerService = services.get(TimerServiceToken);