new Timer(game, autoDestroy)
A Timer is a way to create small re-usable or disposable objects that do nothing but wait for a specific moment in time, and then dispatch an event. You can add as many events to a Timer as you like, each with their own delays. A Timer uses milliseconds as its unit of time. There are 1000 ms in 1 second. So if you want to fire an event every quarter of a second you'd need to set the delay to 250.
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
game |
Phaser.Game | A reference to the currently running game. |
||
autoDestroy |
boolean |
<optional> |
true | A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events). |
- Source:
Members
-
<static, constant> HALF :number
-
Type:
- number
- Source:
-
<static, constant> MINUTE :number
-
Type:
- number
- Source:
-
<static, constant> QUARTER :number
-
Type:
- number
- Source:
-
<static, constant> SECOND :number
-
Type:
- number
- Source:
-
autoDestroy
-
- Source:
Properties:
Name Type Description autoDestroy
boolean A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events).
-
<readonly> duration
-
- Source:
Properties:
Name Type Description duration
number The duration in ms remaining until the next event will occur.
-
events
-
- Source:
Properties:
Name Type Description events
array.<Phaser.TimerEvent> An array holding all of this timers Phaser.TimerEvent objects. Use the methods add, repeat and loop to populate it.
-
<readonly> expired
-
- Default Value:
- false
- Source:
Properties:
Name Type Description expired
boolean An expired Timer is one in which all of its events have been dispatched and none are pending.
-
game
-
- Source:
Properties:
Name Type Description game
Phaser.Game Local reference to game.
-
<readonly> length
-
- Source:
Properties:
Name Type Description length
number The number of pending events in the queue.
-
<readonly> ms
-
- Source:
Properties:
Name Type Description ms
number The duration in milliseconds that this Timer has been running for.
-
<readonly> next
-
- Source:
Properties:
Name Type Description next
number The time at which the next event will occur.
-
<protected, readonly> nextTick
-
- Source:
Properties:
Name Type Description nextTick
number The time the next tick will occur.
-
onComplete
-
- Source:
Properties:
Name Type Description onComplete
Phaser.Signal This signal will be dispatched when this Timer has completed, meaning there are no more events in the queue.
-
<readonly> paused
-
- Default Value:
- false
- Source:
Properties:
Name Type Description 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.
-
running
-
- Default Value:
- false
- Source:
Properties:
Name Type Description running
boolean True if the Timer is actively running. Do not switch this boolean, if you wish to pause the timer then use Timer.pause() instead.
-
<readonly> seconds
-
- Source:
Properties:
Name Type Description seconds
number The duration in seconds that this Timer has been running for.
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. Call Timer.start() once you have added all of the Events you require for this Timer. 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.
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>
The values to be sent to your callback function when it is called.
- Source:
Returns:
The Phaser.TimerEvent object that was created.
- Type
- Phaser.TimerEvent
-
clearPendingEvents()
-
Clears any events from the Timer which have pendingDelete set to true and then resets the private _len and _i values.
- Source:
-
destroy()
-
Destroys this Timer. Any pending Events are not dispatched. The onComplete callbacks won't be called.
- Source:
-
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' milliseconds has passed once the Timer has started running. Call Timer.start() once you have added all of the Events you require for this Timer. 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.
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>
The values to be sent to your callback function when it is called.
- Source:
Returns:
The Phaser.TimerEvent object that was created.
- Type
- Phaser.TimerEvent
-
order()
-
Orders the events on this Timer so they are in tick order. This is called automatically when new events are created.
- Source:
-
pause()
-
Pauses the Timer and all events in the queue.
- Source:
-
remove(event)
-
Removes a pending TimerEvent from the queue.
Parameters:
Name Type Description event
Phaser.TimerEvent The event to remove from the queue.
- Source:
-
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:
-
repeat(delay, repeatCount, callback, callbackContext, arguments) → {Phaser.TimerEvent}
-
Adds a new Event to this Timer that will repeat for the given number of iterations. The event will fire after the given amount of 'delay' milliseconds has passed once the Timer has started running. Call Timer.start() once you have added all of the Events you require for this Timer. 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.
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>
The values to be sent to your callback function when it is called.
- Source:
Returns:
The Phaser.TimerEvent object that was created.
- Type
- Phaser.TimerEvent
-
resume()
-
Resumes the Timer and updates all pending events.
- Source:
-
<protected> sortHandler()
-
Sort handler used by Phaser.Timer.order.
- Source:
-
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:
-
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:
-
<protected> update(time) → {boolean}
-
The main Timer update event, called automatically by the Game clock.
Parameters:
Name Type Description time
number The time from the core game clock.
- Source:
Returns:
True if there are still events waiting to be dispatched, otherwise false if this Timer can be destroyed.
- Type
- boolean