Skip to content

ITimerPlugin

Defined in: plugins/TimerPlugin.ts:492

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

app: IApplication

Defined in: plugins/Plugin.ts:10

IPlugin.app


id: string

Defined in: plugins/Plugin.ts:8

IPlugin.id


onAllTimersPaused: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:524

Signal emitted when all timers are paused.

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

onAllTimersResumed: Signal<() => void>

Defined in: plugins/TimerPlugin.ts:535

Signal emitted when all timers are resumed.

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

onTimerCreated: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:502

Signal emitted when a new timer is created.

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

onTimerDestroyed: Signal<(timer) => void>

Defined in: plugins/TimerPlugin.ts:513

Signal emitted when a timer is destroyed.

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

readonly options: Partial<O>

Defined in: plugins/Plugin.ts:12

IPlugin.options

addSignalConnection(…args): void

Defined in: plugins/Plugin.ts:20

SignalConnection[]

void

IPlugin.addSignalConnection


adjustWorkerTimer(timerId, timeAdjustment): void

Defined in: plugins/TimerPlugin.ts:638

Adjusts the time of a worker timer. This is primarily used internally by the Timer class.

string

The ID of the worker timer to adjust

number

The amount of time to add (positive) or remove (negative) in milliseconds

void

// This is typically called internally by the Timer class
this.app.timers.adjustWorkerTimer(timer.getId(), 5000); // Add 5 seconds

clearSignalConnections(): void

Defined in: plugins/Plugin.ts:22

void

IPlugin.clearSignalConnections


createTimer(options?): Timer

Defined in: plugins/TimerPlugin.ts:556

Creates a new timer with the specified options.

TimerOptions

Configuration options for the timer

Timer

A new Timer instance

// 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(): void

Defined in: plugins/Plugin.ts:18

void

IPlugin.destroy


destroyAllTimers(): void

Defined in: plugins/TimerPlugin.ts:609

Destroys all active timers, both main thread and worker timers . This will remove all timers from the plugin and stop them.

void

this.app.timers.destroyAllTimers();

destroyTimer(timer): void

Defined in: plugins/TimerPlugin.ts:570

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

Timer

The timer instance to destroy

void

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

initialize(options, app): void | Promise<void>

Defined in: plugins/Plugin.ts:14

Partial<O>

IApplication

void | Promise<void>

IPlugin.initialize


pauseAllTimers(): void

Defined in: plugins/TimerPlugin.ts:584

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

void

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

postInitialize(_app): void | Promise<void>

Defined in: plugins/Plugin.ts:16

IApplication

void | Promise<void>

IPlugin.postInitialize


registerCoreFunctions(): void

Defined in: plugins/Plugin.ts:24

void

IPlugin.registerCoreFunctions


registerCoreSignals(): void

Defined in: plugins/Plugin.ts:26

void

IPlugin.registerCoreSignals


resetWorkerTimer(timerId): void

Defined in: plugins/TimerPlugin.ts:623

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

string

The ID of the worker timer to reset

void

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

resumeAllTimers(): void

Defined in: plugins/TimerPlugin.ts:598

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

void

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