new Timer(game, autoDestroy)
A Timer is a way to create small re-usable (or disposable) objects that wait for a specific moment in time, and then run the specified callbacks.
You can many events to a Timer, each with their own delays. A Timer uses milliseconds as its unit of time (there are 1000 ms in 1 second). So a delay to 250 would fire the event every quarter of a second.
Timers are based on real-world (not physics) time, adjusted for game pause durations.
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
game |
Phaser.Game | A reference to the currently running game. |
||
autoDestroy |
boolean |
<optional> |
true | If true, the timer will automatically destroy itself after all the events have been dispatched (assuming no looping events). |
- Source - time/Timer.js, line 21
Members
-
<static, constant> HALF :integer
-
Number of milliseconds in half a second.
- Source - time/Timer.js, line 175
-
<static, constant> MINUTE :integer
-
Number of milliseconds in a minute.
- Source - time/Timer.js, line 161
-
<static, constant> QUARTER :integer
-
Number of milliseconds in a quarter of a second.
- Source - time/Timer.js, line 182
-
<static, constant> SECOND :integer
-
Number of milliseconds in a second.
- Source - time/Timer.js, line 168
-
autoDestroy :boolean
-
If true, the timer will automatically destroy itself after all the events have been dispatched (assuming no looping events).
- Source - time/Timer.js, line 45
-
<readonly> duration :number
-
The duration in ms remaining until the next event will occur.
- Source - time/Timer.js, line 676
-
<internal> elapsed :number
-
Elapsed time since the last frame (in ms).
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 58
-
events :Array.<Phaser.TimerEvent>
-
An array holding all of this timers Phaser.TimerEvent objects. Use the methods add, repeat and loop to populate it.
Type:
- Array.<Phaser.TimerEvent>
- Source - time/Timer.js, line 63
-
<readonly> expired :boolean
-
An expired Timer is one in which all of its events have been dispatched and none are pending.
- Default Value:
- false
- Source - time/Timer.js, line 52
-
<internal> game :Phaser.Game
-
Local reference to game.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 29
-
<readonly> length :number
-
The number of pending events in the queue.
- Source - time/Timer.js, line 698
-
<readonly> ms :number
-
The duration in milliseconds that this Timer has been running for.
- Source - time/Timer.js, line 711
-
<readonly> next :number
-
The time at which the next event will occur.
- Source - time/Timer.js, line 663
-
<internal, readonly> nextTick :number
-
The time the next tick will occur.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 79
-
onComplete :Phaser.Signal
-
This signal will be dispatched when this Timer has completed which means that there are no more events in the queue.
The signal is supplied with one argument,
timer
, which is this Timer object.- Source - time/Timer.js, line 72
-
<readonly> paused :boolean
-
The paused state of the Timer. You can pause the timer by calling Timer.pause() and Timer.resume() or by the game pausing.
- Default Value:
- false
- Source - time/Timer.js, line 91
-
<readonly> running :boolean
-
True if the Timer is actively running.
Do not modify this boolean - use pause (and resume) to pause the timer.
- Default Value:
- false
- Source - time/Timer.js, line 39
-
<readonly> seconds :number
-
The duration in seconds that this Timer has been running for.
- Source - time/Timer.js, line 733
-
timeCap :number
-
If the difference in time between two frame updates exceeds this value, the event times are reset to avoid catch-up situations.
- Source - time/Timer.js, line 84
Methods
-
add(delay, callback, callbackContext, arguments) → {Phaser.TimerEvent}
-
Adds a new Event to this Timer.
The event will fire after the given amount of
delay
in milliseconds has passed, once the Timer has started running. The delay is in relation to when the Timer starts, not the time it was added. If the Timer is already running the delay will be calculated based on the timers current time.Make sure to call start after adding all of the Events you require for this Timer.
Parameters:
Name Type Argument Description delay
number The number of milliseconds that should elapse before the callback is invoked.
callback
function The callback that will be called when the Timer event occurs.
callbackContext
object The context in which the callback will be called.
arguments
* <repeatable>
Additional arguments that will be supplied to the callback.
Returns:
The Phaser.TimerEvent object that was created.
- Source - time/Timer.js, line 228
-
<internal> adjustEvents()
-
Adjusts the time of all pending events and the nextTick by the given baseTime.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 551
-
<internal> clearPendingEvents()
-
Clears any events from the Timer which have pendingDelete set to true and then resets the private _len and _i values.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 391
-
destroy()
-
Destroys this Timer. Any pending Events are not dispatched. The onComplete callbacks won't be called.
- Source - time/Timer.js, line 645
-
loop(delay, callback, callbackContext, arguments) → {Phaser.TimerEvent}
-
Adds a new looped Event to this Timer that will repeat forever or until the Timer is stopped.
The event will fire after the given amount of
delay
in milliseconds has passed, once the Timer has started running. The delay is in relation to when the Timer starts, not the time it was added. If the Timer is already running the delay will be calculated based on the timers current time.Make sure to call start after adding all of the Events you require for this Timer.
Parameters:
Name Type Argument Description delay
number The number of milliseconds that should elapse before the Timer will call the given callback.
callback
function The callback that will be called when the Timer event occurs.
callbackContext
object The context in which the callback will be called.
arguments
* <repeatable>
Additional arguments that will be supplied to the callback.
Returns:
The Phaser.TimerEvent object that was created.
- Source - time/Timer.js, line 271
-
<internal> order()
-
Orders the events on this Timer so they are in tick order. This is called automatically when new events are created.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 353
-
pause()
-
Pauses the Timer and all events in the queue.
- Source - time/Timer.js, line 509
-
remove(event)
-
Removes a pending TimerEvent from the queue.
Parameters:
Name Type Description event
Phaser.TimerEvent The event to remove from the queue.
- Source - time/Timer.js, line 333
-
removeAll()
-
Removes all Events from this Timer and all callbacks linked to onComplete, but leaves the Timer running.
The onComplete callbacks won't be called.- Source - time/Timer.js, line 630
-
repeat(delay, repeatCount, callback, callbackContext, arguments) → {Phaser.TimerEvent}
-
Adds a new TimerEvent that will always play through once and then repeat for the given number of iterations.
The event will fire after the given amount of
delay
in milliseconds has passed, once the Timer has started running. The delay is in relation to when the Timer starts, not the time it was added. If the Timer is already running the delay will be calculated based on the timers current time.Make sure to call start after adding all of the Events you require for this Timer.
Parameters:
Name Type Argument Description delay
number The number of milliseconds that should elapse before the Timer will call the given callback.
repeatCount
number The number of times the event will repeat once is has finished playback. A repeatCount of 1 means it will repeat itself once, playing the event twice in total.
callback
function The callback that will be called when the Timer event occurs.
callbackContext
object The context in which the callback will be called.
arguments
* <repeatable>
Additional arguments that will be supplied to the callback.
Returns:
The Phaser.TimerEvent object that was created.
- Source - time/Timer.js, line 249
-
resume()
-
Resumes the Timer and updates all pending events.
- Source - time/Timer.js, line 589
-
start(delay)
-
Starts this Timer running.
Parameters:
Name Type Argument Default Description delay
number <optional>
0 The number of milliseconds that should elapse before the Timer will start.
- Source - time/Timer.js, line 292
-
stop(clearEvents)
-
Stops this Timer from running. Does not cause it to be destroyed if autoDestroy is set to true.
Parameters:
Name Type Argument Default Description clearEvents
boolean <optional>
true If true all the events in Timer will be cleared, otherwise they will remain.
- Source - time/Timer.js, line 315
-
<internal> update(time) → {boolean}
-
The main Timer update event, called automatically by Phaser.Time.update.
Parameters:
Name Type Description time
number The time from the core game clock.
Returns:
boolean -True if there are still events waiting to be dispatched, otherwise false if this Timer can be destroyed.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - time/Timer.js, line 414