# Class: Timer

## Phaser. Timer

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.

#### 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:
    - time/Timer.js, line 18

### Members

#### <static, constant> HALF :number

  ##### Type:

  - number
  Source:
    - time/Timer.js, line 159

#### <static, constant> MINUTE :number

  ##### Type:

  - number
  Source:
    - time/Timer.js, line 147

#### <static, constant> QUARTER :number

  ##### Type:

  - number
  Source:
    - time/Timer.js, line 165

#### <static, constant> SECOND :number

  ##### Type:

  - number
  Source:
    - time/Timer.js, line 153

#### autoDestroy

  ##### 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).

  Source:
    - time/Timer.js, line 36

#### <readonly> duration

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `duration` | number | The duration in ms remaining until the next event will occur.

  Source:
    - time/Timer.js, line 643

#### <protected> elapsed

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `elapsed` | number | Elapsed time since the last frame (in ms).

  Source:
    - time/Timer.js, line 49

#### events

  ##### 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.

  Source:
    - time/Timer.js, line 54

#### <readonly> expired

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `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 43

#### game

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game | Local reference to game.

  Source:
    - time/Timer.js, line 25

#### <readonly> length

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `length` | number | The number of pending events in the queue.

  Source:
    - time/Timer.js, line 665

#### <readonly> ms

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `ms` | number | The duration in milliseconds that this Timer has been running for.

  Source:
    - time/Timer.js, line 678

#### <readonly> next

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `next` | number | The time at which the next event will occur.

  Source:
    - time/Timer.js, line 630

#### <protected, readonly> nextTick

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `nextTick` | number | The time the next tick will occur.

  Source:
    - time/Timer.js, line 66

#### onComplete

  ##### 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.

  Source:
    - time/Timer.js, line 59

#### <readonly> paused

  ##### 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.

  Default Value:
    - false
  Source:
    - time/Timer.js, line 78

#### running

  ##### 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.

  Default Value:
    - false
  Source:
    - time/Timer.js, line 31

#### <readonly> seconds

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `seconds` | number | The duration in seconds that this Timer has been running for.

  Source:
    - time/Timer.js, line 691

#### timeCap

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `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 71

### 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:
    - time/Timer.js, line 206

  ##### Returns:

  The Phaser.TimerEvent object that was created.

  Type
    Phaser.TimerEvent

#### adjustEvents()

  Adjusts the time of all pending events and the nextTick by the given baseTime.

  Source:
    - time/Timer.js, line 519

#### clearPendingEvents()

  Clears any events from the Timer which have pendingDelete set to true and then resets the private _len and _i values.

  Source:
    - time/Timer.js, line 360

#### destroy()

  Destroys this Timer. Any pending Events are not dispatched. The onComplete callbacks won't be called.

  Source:
    - time/Timer.js, line 612

#### 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:
    - time/Timer.js, line 244

  ##### 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:
    - time/Timer.js, line 324

#### pause()

  Pauses the Timer and all events in the queue.

  Source:
    - time/Timer.js, line 477

#### 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 304

#### 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 597

#### 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' 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:
    - time/Timer.js, line 224

  ##### Returns:

  The Phaser.TimerEvent object that was created.

  Type
    Phaser.TimerEvent

#### resume()

  Resumes the Timer and updates all pending events.

  Source:
    - time/Timer.js, line 556

#### <protected> sortHandler()

  Sort handler used by Phaser.Timer.order.

  Source:
    - time/Timer.js, line 340

#### 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 263

#### 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 286

#### <protected> 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.

  Source:
    - time/Timer.js, line 382

  ##### Returns:

  True if there are still events waiting to be dispatched, otherwise false if this Timer can be destroyed.

  Type
    boolean