Time and Timers
Clock plugin, TimerEvent, delays, loops, Timeline event sequencing, pausing time, time scale, and delta time in Phaser 4.
Key source paths: ,
,
,
,
Related skills: ../scenes/SKILL.md, ../tweens/SKILL.md
Quick Start
js
// In a Scene's create() method:
// One-shot delayed call (fires once after 1 second)
this.time.delayedCall(1000, () => {
console.log('One second later');
});
// Repeating timer (fires 5 times, once every 500ms)
this.time.addEvent({
delay: 500,
callback: () => { console.log('tick'); },
repeat: 4 // 4 repeats = 5 total fires
});
// Infinite loop timer
this.time.addEvent({
delay: 1000,
callback: this.spawnEnemy,
callbackScope: this,
loop: true
});
is the scene's
instance (registered as the
plugin under the key
). It creates and manages
objects that fire callbacks based on game time.
Core Concepts
Clock (this.time)
The Clock is a Scene-level plugin that tracks game time and updates all of its TimerEvents each frame. Key properties:
- -- current time in ms (equivalent to passed to the scene method).
- -- timestamp when the scene started.
- -- multiplier applied to delta time. Default . Values above 1 speed up all timers; below 1 slow them down; freezes time.
- -- when , no TimerEvents are updated.
The Clock listens to
(to flush pending additions/removals) and
(to tick active events). It is automatically shut down and destroyed with the scene.
TimerEvent
A TimerEvent accumulates elapsed time each frame:
elapsed += delta * clock.timeScale * event.timeScale
. When
, the callback fires. After all repeats are exhausted the event is removed from the Clock on the next frame.
Key properties set via config:
,
,
,
,
,
,
,
,
.
Timeline (this.add.timeline)
A Timeline is a sequencer for scheduling actions at specific points in time. Unlike the Clock (which manages independent timers), a Timeline runs a linear sequence of events keyed by absolute or relative timestamps.
js
const timeline = this.add.timeline([
{ at: 0, run: () => { /* immediate */ } },
{ at: 1000, run: () => { /* at 1s */ } },
{ at: 2500, tween: { targets: sprite, alpha: 0, duration: 500 } }
]);
timeline.play();
Timelines always start
paused. You must call
to start them. They are created via the GameObjectFactory and destroyed automatically when the scene shuts down.
Common Patterns
Delayed Call
js
// Shorthand -- fires once, no repeat
this.time.delayedCall(2000, () => {
this.scene.start('GameOver');
});
Repeating Timer with Finite Count
js
// repeat: 9 means 10 total fires (1 initial + 9 repeats)
const timer = this.time.addEvent({
delay: 200,
callback: this.fireBullet,
callbackScope: this,
repeat: 9
});
// Check progress
timer.getRepeatCount(); // repeats remaining
timer.getOverallProgress(); // 0..1 across all repeats
Infinite Loop
js
const spawner = this.time.addEvent({
delay: 3000,
callback: this.spawnWave,
callbackScope: this,
loop: true
});
// Stop it later
spawner.remove(); // or spawner.paused = true to pause
Setting
is equivalent to
.
First-Fire Shortcut with startAt
js
// First fire happens quickly (after 100ms), then every 2s
this.time.addEvent({
delay: 2000,
callback: this.heartbeat,
callbackScope: this,
loop: true,
startAt: 1900 // pre-fill elapsed so first fire is at 100ms
});
Stopping and Removing Timers
js
const timer = this.time.addEvent({ delay: 1000, loop: true, callback: fn });
// Option 1: Remove from clock (schedules removal next frame)
timer.remove(); // silently expires
timer.remove(true); // fires callback one last time, then expires
// Option 2: Remove via Clock
this.time.removeEvent(timer);
// Option 3: Remove all timers
this.time.removeAllEvents();
Pausing and Resuming
js
// Pause the entire Clock (all timers freeze)
this.time.paused = true;
this.time.paused = false;
// Pause a single timer
timer.paused = true;
timer.paused = false;
Time Scale (Slow Motion / Fast Forward)
js
// Slow all timers in this scene to half speed
this.time.timeScale = 0.5;
// Speed up a single timer to 2x
timer.timeScale = 2;
// Combined: effective scale = clock.timeScale * event.timeScale
// So 0.5 * 2 = 1x for that specific timer
Reading Timer State
js
timer.getProgress(); // 0..1 for current iteration
timer.getOverallProgress(); // 0..1 across all repeats
timer.getElapsed(); // ms elapsed this iteration
timer.getElapsedSeconds(); // seconds elapsed this iteration
timer.getRemaining(); // ms until next fire
timer.getRemainingSeconds(); // seconds until next fire
timer.getOverallRemaining(); // ms until final fire
timer.getOverallRemainingSeconds(); // seconds until final fire
timer.getRepeatCount(); // repeats left
Timeline: Sequencing Events
js
const timeline = this.add.timeline([
{
at: 0,
run: () => { this.title.setAlpha(1); },
sound: 'intro'
},
{
at: 2000,
tween: { targets: this.title, y: 100, duration: 1000 },
sound: { key: 'whoosh', config: { volume: 0.5 } }
},
{
at: 4000,
set: { alpha: 0 },
target: this.title,
event: 'INTRO_DONE'
}
]);
timeline.on('INTRO_DONE', (target) => { /* custom event */ });
timeline.on('complete', (tl) => { /* all events ran */ });
timeline.play();
Timeline: Relative Timing with and
js
const timeline = this.add.timeline([
{ at: 1000, run: stepOne }, // absolute: 1s from start
{ from: 500, run: stepTwo }, // relative: 500ms after previous (1.5s)
{ from: 1000, run: stepThree } // relative: 1000ms after previous (2.5s)
]);
- -- absolute ms from timeline start (default 0).
- -- offset from current time (useful when adding events to a running timeline).
- -- offset from the previous event's start time.
Priority:
overrides
, which overrides
.
Timeline: Conditional Events
js
const timeline = this.add.timeline([
{
at: 5000,
if: () => this.player.health > 0,
run: () => { this.showBonusRound(); }
}
]);
If the
callback returns
, the event is skipped (marked complete but actions are not executed).
Timeline: Looping
js
const timeline = this.add.timeline([
{ at: 0, run: () => console.log('start') },
{ at: 1000, run: () => console.log('end') }
]);
timeline.repeat().play(); // infinite loop
timeline.repeat(3).play(); // loop 3 additional times (4 total runs)
timeline.repeat(false).play(); // no looping
The
callback on a TimelineEventConfig fires on repeat iterations (not the first run).
Timeline: Pause, Resume, Stop, Reset
js
timeline.pause(); // freezes elapsed counter and pauses spawned tweens
timeline.resume(); // resumes elapsed counter and unpauses spawned tweens
timeline.stop(); // sets paused=true, complete=true
timeline.reset(); // elapsed=0, all events marked incomplete, starts playing
timeline.isPlaying(); // true if not paused and not complete
timeline.getProgress(); // 0..1 based on events completed / total events
Timeline: Time Scale
js
timeline.timeScale = 2; // double speed
timeline.timeScale = 0.5; // half speed
Note: Timeline
does not affect tweens created by the timeline. Set tween timeScale separately.
Timeline: Full Cutscene Example
Timelines excel at choreographing cutscenes with mixed actions (callbacks, tweens, sounds, property sets, and custom events):
js
class CutsceneScene extends Phaser.Scene {
create() {
const timeline = this.add.timeline([
{
at: 0,
run: () => { console.log('Start!'); }
},
{
at: 1000,
tween: {
targets: this.player,
x: 400,
duration: 500,
ease: 'Power2'
}
},
{
at: 1500,
sound: 'doorOpen'
},
{
at: 2000,
set: { visible: true },
target: this.door
},
{
from: 500,
run: () => { console.log('Relative timing'); }
},
{
at: 5000,
event: 'cutsceneDone',
stop: true
}
]);
timeline.on('cutsceneDone', () => {
this.scene.start('GameScene');
});
timeline.play();
}
}
Timer Reset and Reuse
A completed
can be reset with a new config and re-added to the Clock:
js
const timer = this.time.addEvent({
delay: 1000,
callback: this.phase1,
callbackScope: this,
repeat: 2
});
// Later, after it completes, reconfigure and re-add:
timer.reset({
delay: 500,
callback: this.phase2,
callbackScope: this,
repeat: 4
});
this.time.addEvent(timer); // must re-add; reset() alone does not schedule it
Configuration Reference
TimerEventConfig
| Property | Type | Default | Description |
|---|
| number | | Delay in ms before the callback fires |
| number | | Times to repeat after first fire. Use for infinite |
| boolean | | If , repeats indefinitely (same as ) |
| function | -- | Function called when the timer fires |
| any | TimerEvent | The context for the callback |
| Array | | Extra arguments passed to the callback |
| number | | Per-event time multiplier |
| number | | Pre-fill elapsed time in ms (makes first fire happen sooner) |
| boolean | | Start the timer in a paused state |
TimelineEventConfig
| Property | Type | Default | Description |
|---|
| number | | Absolute time in ms from timeline start |
| number | -- | Offset from current elapsed (overrides ) |
| number | -- | Offset from previous event time (overrides and ) |
| function | -- | Callback invoked when event fires |
| function | -- | Callback invoked on repeat iterations (not first run) |
| function | -- | Guard function; return to skip the event |
| string | -- | Event name emitted on the Timeline instance |
| any | -- | Scope for // and target for |
| object | -- | Key-value pairs applied to when event fires |
| TweenConfig | -- | Tween config or instance created when event fires |
| string/object | -- | Sound key or to play |
| boolean | | Remove this event after it fires |
| boolean | | Stop the entire timeline when this event fires |
TimelineEvent (Internal)
The processed event object stored in
. Extends config with:
| Property | Type | Description |
|---|
| boolean | Whether this event has fired |
| number | Resolved absolute time in ms |
| number | How many times this event has repeated |
| Tween/TweenChain | Reference to spawned tween (if any) |
Events
Timeline Events
| Event | Constant | Listener Signature | Fired When |
|---|
| Phaser.Time.Events.COMPLETE
| | All timeline events have been run |
Custom events via the
property in TimelineEventConfig are also emitted on the Timeline instance with signature
.
TimerEvent / Clock
TimerEvent and Clock do not emit EventEmitter events. Timers use the
property directly. The Clock is managed by scene lifecycle events (
,
,
,
).
API Quick Reference
Clock (this.time)
| Method | Signature | Returns | Description |
|---|
| | | Create and schedule a timer event |
| (delay, callback, args?, scope?)
| | Shorthand for a one-shot delayed call |
| | | Remove specific timer(s) |
| | | Schedule removal of all active timers |
| | | Clear timers not yet added to active list |
| Property | Type | Description |
|---|
| number | Current clock time in ms |
| number | Time the scene started |
| number | Delta multiplier for all timers |
| boolean | Freeze all timers |
TimerEvent
| Method | Returns | Description |
|---|
| number | 0..1 progress of current iteration |
| number | 0..1 progress across all repeats |
| number | Elapsed ms this iteration |
| number | Elapsed seconds this iteration |
| number | Ms until next fire |
| number | Seconds until next fire |
| number | Ms until final fire |
getOverallRemainingSeconds()
| number | Seconds until final fire |
| number | Repeats remaining |
remove(dispatchCallback?)
| void | Expire the timer (optionally fire callback) |
| TimerEvent | Reinitialize with new config |
| void | Null out callback references |
Timeline (this.add.timeline)
| Method | Signature | Returns | Description |
|---|
| | | Append events to the timeline |
| | | Start playing (default resets to start) |
| | | Pause timeline and spawned tweens |
| | | Resume timeline and spawned tweens |
| | | Stop (sets paused + complete) |
| | | Reset elapsed and all events to incomplete |
| | | Set loop count (-1/true=infinite, false=none) |
| | | Remove all events, reset elapsed, pause |
| | boolean | True if not paused and not complete |
| | number | 0..1 based on completed event count |
| Property | Type | Description |
|---|
| number | Current elapsed time in ms |
| number | Delta multiplier (does not affect spawned tweens) |
| boolean | Whether timeline is paused |
| boolean | Whether all events have run |
| number | Number of additional loops (0=none, -1=infinite) |
| number | Current loop iteration |
| number | Count of events that have fired |
| TimelineEvent[] | The internal event array |
Gotchas
- repeat vs total fires. means 5 total callback invocations (1 initial + 4 repeats). This is a common off-by-one source.
- Zero delay with repeat throws. A with and any repeat/loop will throw
'TimerEvent infinite loop created via zero delay'
.
- Timelines start paused. You must call after creation. Forgetting this is a frequent mistake.
- Timeline timeScale does not affect tweens. Setting only scales the timeline's own elapsed counter. Tweens created by the timeline run at their own speed. Set tween separately or use the TweenManager.
- once events are removed permanently. Timeline events with are spliced out after firing and will not reappear on or when looping.
- Timer additions are deferred. pushes to a pending list processed in . The timer will not be active until the next frame.
- Clock paused vs timeScale 0. Both freeze timers, but skips the update loop entirely, while still runs the loop with zero delta. Prefer for a full freeze.
- callbackScope default. If you omit , the TimerEvent itself becomes inside the callback, not the scene. Use arrow functions or pass explicitly.
- Reusing TimerEvent instances. You can pass a object to , but it must not be in a completed state. The Clock will reset its elapsed and dispatch state.
- Timeline chains. Each offset is relative to the previous event's resolved time, not the previous value. Events are processed in array order.
- Scene pause pauses the Timeline. When a scene is paused (), the Timeline's update loop stops too, since Timeline updates are driven by the scene's update step.
- does not re-add to Clock. Calling reinitializes the timer but does not schedule it. You must call
this.time.addEvent(timer)
again after resetting.
Source File Map
| File | Description |
|---|
| Scene Clock plugin -- creates, updates, removes TimerEvents |
| Individual timer -- delay, repeat, loop, progress tracking |
| Event sequencer -- scheduled actions, tweens, sounds, looping |
src/time/typedefs/TimerEventConfig.js
| Config typedef for |
src/time/typedefs/TimelineEventConfig.js
| Config typedef for |
src/time/typedefs/TimelineEvent.js
| Internal event object typedef |
src/time/events/COMPLETE_EVENT.js
| event constant for Timeline |
| Events namespace barrel file |