Skip to content

ITimerPlugin

Defined in: plugins/TimerPlugin.ts:417

Interface for the Timer Plugin which manages both main-thread and web worker timers.

The Timer Plugin provides functionality for:

  • Creating and managing countdown and count-up timers
  • Running timers in a web worker for better performance
  • Pausing and resuming all timers
  • Handling timer lifecycle events

Example

// Create a 5-second countdown timer
const countdown = this.app.timers.createTimer({
duration: 5000,
autoStart: true,
onTick: (remaining) => {
console.log(`Time remaining: ${formatTime(remaining, 'mm:ss')}`);
},
onComplete: () => {
console.log('Timer completed!');
}
});
// Create a count-up timer in a web worker
const stopwatch = this.app.timers.createTimer({
useWorker: true,
workerInterval: 100, // Update every 100ms
onTick: (elapsed) => {
console.log(`Time elapsed: ${formatTime(elapsed, 'mm:ss')}`);
}
});
// Pause all timers
this.app.timers.pauseAllTimers();
// Resume all timers
this.app.timers.resumeAllTimers();

Extends

Properties

app

app: IApplication

Defined in: plugins/Plugin.ts:10

Inherited from

IPlugin.app


id

id: string

Defined in: plugins/Plugin.ts:8

Inherited from

IPlugin.id


onAllTimersPaused

onAllTimersPaused: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:449

Signal emitted when all timers are paused.

Example

this.app.timers.onAllTimersPaused.connect(() => {
console.log('All timers paused');
});

onAllTimersResumed

onAllTimersResumed: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:460

Signal emitted when all timers are resumed.

Example

this.app.timers.onAllTimersResumed.connect(() => {
console.log('All timers resumed');
});

onTimerCreated

onTimerCreated: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:427

Signal emitted when a new timer is created.

Example

this.app.timers.onTimerCreated.connect((timer) => {
console.log(`New timer created with ID: ${timer.getId()}`);
});

onTimerDestroyed

onTimerDestroyed: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:438

Signal emitted when a timer is destroyed.

Example

this.app.timers.onTimerDestroyed.connect((timer) => {
console.log(`Timer destroyed: ${timer.getId()}`);
});

Methods

addSignalConnection()

addSignalConnection(…args): void

Defined in: plugins/Plugin.ts:18

Parameters

args

SignalConnection[]

Returns

void

Inherited from

IPlugin.addSignalConnection


clearSignalConnections()

clearSignalConnections(): void

Defined in: plugins/Plugin.ts:20

Returns

void

Inherited from

IPlugin.clearSignalConnections


createTimer()

createTimer(options?): Timer

Defined in: plugins/TimerPlugin.ts:481

Creates a new timer with the specified options.

Parameters

options?

TimerOptions

Configuration options for the timer

Returns

Timer

A new Timer instance

Example

// Create a looping timer that runs in a web worker
const timer = this.app.timers.createTimer({
duration: 1000,
loop: true,
useWorker: true,
onTick: (remaining) => {
console.log(`${remaining}ms remaining`);
}
});

destroy()

destroy(): void

Defined in: plugins/Plugin.ts:16

Returns

void

Inherited from

IPlugin.destroy


destroyTimer()

destroyTimer(timer): void

Defined in: plugins/TimerPlugin.ts:495

Destroys a timer, cleaning up all resources and removing it from the plugin.

Parameters

timer

Timer

The timer instance to destroy

Returns

void

Example

const timer = this.app.timers.createTimer({ duration: 5000 });
// ... later ...
this.app.timers.destroyTimer(timer);

initialize()

initialize(_app, options?): void | Promise<void>

Defined in: plugins/Plugin.ts:12

Parameters

_app

IApplication

options?

any

Returns

void | Promise<void>

Inherited from

IPlugin.initialize


pauseAllTimers()

pauseAllTimers(): void

Defined in: plugins/TimerPlugin.ts:509

Pauses all active timers, both main thread and worker timers. Timers will maintain their current state and can be resumed later.

Returns

void

Example

// Pause all timers when the game loses focus
window.onblur = () => {
this.app.timers.pauseAllTimers();
};

postInitialize()

postInitialize(_app): void | Promise<void>

Defined in: plugins/Plugin.ts:14

Parameters

_app

IApplication

Returns

void | Promise<void>

Inherited from

IPlugin.postInitialize


registerCoreFunctions()

registerCoreFunctions(): void

Defined in: plugins/Plugin.ts:22

Returns

void

Inherited from

IPlugin.registerCoreFunctions


registerCoreSignals()

registerCoreSignals(): void

Defined in: plugins/Plugin.ts:24

Returns

void

Inherited from

IPlugin.registerCoreSignals


resetWorkerTimer()

resetWorkerTimer(timerId): void

Defined in: plugins/TimerPlugin.ts:537

Resets a worker timer to its initial state. This is primarily used internally by the Timer class.

Parameters

timerId

string

The ID of the worker timer to reset

Returns

void

Example

// This is typically called internally by the Timer class
this.app.timers.resetWorkerTimer(timer.getId());

resumeAllTimers()

resumeAllTimers(): void

Defined in: plugins/TimerPlugin.ts:523

Resumes all paused timers, both main thread and worker timers. Timers will continue from their paused state.

Returns

void

Example

// Resume all timers when the game regains focus
window.onfocus = () => {
this.app.timers.resumeAllTimers();
};