Timer
Timer system for scheduling delayed and periodic callbacks.
The Timer class provides a central timing system that tracks application time and allows scheduling callbacks to run after delays or at regular intervals. All timers are synchronized with the main application update loop.
Time tracking:
Timer.now
: Application time in seconds since startupTimer.timestamp
: Unix timestamp synchronized with application time
Basic usage:
// Run callback after 2 seconds
Timer.delay(this, 2.0, () -> {
trace("2 seconds elapsed");
});
// Run callback every 0.5 seconds
var cancel = Timer.interval(this, 0.5, () -> {
trace("Tick!");
});
// Cancel the interval later
cancel();
Timers are automatically cancelled when their owner entity is destroyed, preventing memory leaks and null reference errors.
Static Members
now: Float
Current time, relative to app. (number of active seconds since app was started)
This value is incremented by the frame delta each update, providing a consistent time reference for the entire application.
timestamp: Float
Current unix time synchronized with ceramic Timer.
Timer.now
and Timer.timestamp
are guaranteed to get incremented
exactly at the same rate, except when app frame real delta > 1s
(number of seconds since January 1st, 1970)
Useful for timestamping events or synchronizing with external systems.
startTimestamp: Float
The unix timestamp when the application started. Used as the base for calculating the current timestamp.
Execute a callback after the given delay in seconds.
Name | Type | Description |
---|---|---|
owner |
Null<Entity> | Optional entity that owns this timer. If provided and the entity is destroyed, the timer is automatically cancelled. |
seconds |
Float | The delay in seconds before executing the callback |
callback |
Function | The function to execute after the delay |
Returns | Description |
---|---|
Function | A function that can be called to cancel this timer delay * Example: haxe // Simple delay Timer.delay(this, 1.0, () -> trace("1 second passed")); * // With cancellation var cancel = Timer.delay(this, 5.0, () -> startBossFight()); // Cancel if player dies if (playerDied) cancel(); |
Execute a callback periodically at the given interval in seconds.
Name | Type | Description |
---|---|---|
owner |
Null<Entity> | Optional entity that owns this timer. If provided and the entity is destroyed, the timer is automatically cancelled. |
seconds |
Float | The interval in seconds between each callback execution |
callback |
Function | The function to execute at each interval |
Returns | Description |
---|---|
Function | A function that can be called to cancel this timer interval * Example: haxe // Update every frame (60 FPS) Timer.interval(this, 1/60, () -> updatePhysics()); * // Spawn enemy every 2 seconds var spawnTimer = Timer.interval(this, 2.0, () -> spawnEnemy()); * // Stop spawning after 10 seconds Timer.delay(this, 10.0, () -> spawnTimer()); |
Private Members
callbacks: Array<TimerCallback>
next: Float
Internal method called by App to update timer state.
Name | Type | Description |
---|---|---|
delta |
Float | The frame time delta in seconds |
realDelta |
Float | The real time delta in seconds (unaffected by time scale) |
flush(): Void
Process all pending timer callbacks that are ready to execute. Called automatically when timer callbacks are due.
Internal method to schedule a timer callback.
Name | Type | Description |
---|---|---|
owner |
Entity | The entity that owns this timer (for auto-cleanup) |
seconds |
Float | Initial delay before first execution |
callback |
Function | The function to execute |
interval |
Float | For repeating timers, the interval between executions. -1 for one-shot timers. |
Returns | Description |
---|---|
Function | A function to cancel the timer |