Skip to content

TimerPlugin

Defined in: plugins/TimerPlugin.ts:540

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

Implements

Constructors

new TimerPlugin()

new TimerPlugin(): TimerPlugin

Defined in: plugins/TimerPlugin.ts:557

Returns

TimerPlugin

Overrides

Plugin.constructor

Properties

__dill_pixel_method_binding_root

__dill_pixel_method_binding_root: boolean

Defined in: plugins/Plugin.ts:37

Inherited from

Plugin.__dill_pixel_method_binding_root


id

id: string = 'Plugin'

Defined in: plugins/Plugin.ts:40

Implementation of

ITimerPlugin.id

Inherited from

Plugin.id


onAllTimersPaused

readonly onAllTimersPaused: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:554

Signal emitted when all timers are paused.

Example

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

Implementation of

ITimerPlugin.onAllTimersPaused


onAllTimersResumed

readonly onAllTimersResumed: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:555

Signal emitted when all timers are resumed.

Example

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

Implementation of

ITimerPlugin.onAllTimersResumed


onTimerCreated

readonly onTimerCreated: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:552

Signal emitted when a new timer is created.

Example

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

Implementation of

ITimerPlugin.onTimerCreated


onTimerDestroyed

readonly onTimerDestroyed: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:553

Signal emitted when a timer is destroyed.

Example

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

Implementation of

ITimerPlugin.onTimerDestroyed

Accessors

app

Get Signature

get app(): T

Defined in: plugins/Plugin.ts:45

Returns

T

Implementation of

ITimerPlugin.app

Inherited from

Plugin.app


hasWorkerSupport

Get Signature

get hasWorkerSupport(): boolean

Defined in: plugins/TimerPlugin.ts:548

Returns

boolean

Methods

addSignalConnection()

addSignalConnection(…args): void

Defined in: plugins/Plugin.ts:71

Add signal connections to the container.

Parameters

args

SignalConnection[]

The signal connections to add.

Returns

void

Implementation of

ITimerPlugin.addSignalConnection

Inherited from

Plugin.addSignalConnection


clearSignalConnections()

clearSignalConnections(): void

Defined in: plugins/Plugin.ts:77

Returns

void

Implementation of

ITimerPlugin.clearSignalConnections

Inherited from

Plugin.clearSignalConnections


createTimer()

createTimer(options?): Timer

Defined in: plugins/TimerPlugin.ts:675

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`);
}
});

Implementation of

ITimerPlugin.createTimer


destroy()

destroy(): void

Defined in: plugins/TimerPlugin.ts:658

Returns

void

Implementation of

ITimerPlugin.destroy

Overrides

Plugin.destroy


destroyTimer()

destroyTimer(timer): void

Defined in: plugins/TimerPlugin.ts:689

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

Implementation of

ITimerPlugin.destroyTimer


initialize()

initialize(app): Promise<void>

Defined in: plugins/TimerPlugin.ts:569

Parameters

app

IApplication

Returns

Promise<void>

Implementation of

ITimerPlugin.initialize

Overrides

Plugin.initialize


pauseAllTimers()

pauseAllTimers(): void

Defined in: plugins/TimerPlugin.ts:703

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();
};

Implementation of

ITimerPlugin.pauseAllTimers


postInitialize()

postInitialize(_app): void | Promise<void>

Defined in: plugins/Plugin.ts:60

Parameters

_app

IApplication

Returns

void | Promise<void>

Implementation of

ITimerPlugin.postInitialize

Inherited from

Plugin.postInitialize


resetWorkerTimer()

resetWorkerTimer(timerId): void

Defined in: plugins/TimerPlugin.ts:639

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());

Implementation of

ITimerPlugin.resetWorkerTimer


resumeAllTimers()

resumeAllTimers(): void

Defined in: plugins/TimerPlugin.ts:711

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();
};

Implementation of

ITimerPlugin.resumeAllTimers


startWorkerTimer()

startWorkerTimer(timerId, options): void

Defined in: plugins/TimerPlugin.ts:614

Parameters

timerId

string

options

TimerOptions

Returns

void


stopWorkerTimer()

stopWorkerTimer(timerId): void

Defined in: plugins/TimerPlugin.ts:629

Parameters

timerId

string

Returns

void