Phaser API Documentation

  Version: 
Filter

The core runner class that Phaser uses to handle the game loop. It can use either Request Animation Frame, or SetTimeout, based on browser support and config settings, to create a continuous loop within the browser.

Each time the loop fires, TimeStep.step is called and this is then passed onto the core Game update loop, it is the core heartbeat of your game. It will fire as often as Request Animation Frame is capable of handling on the target device.

Note that there are lots of situations where a browser will stop updating your game. Such as if the player switches tabs, or covers up the browser window with another application. In these cases, the 'heartbeat' of your game will pause, and only resume when focus is returned to it by the player. There is no way to avoid this situation, all you can do is use the visibility events the browser, and Phaser, provide to detect when it has happened and then gracefully recover.

Constructor:

new TimeStep(game, config)

Parameters:

name type description
game Phaser.Game

A reference to the Phaser.Game instance that owns this Time Step.

config Phaser.Types.Core.FPSConfig
Since: 3.0.0
Source: src/core/TimeStep.js (Line 14)

Members

<private> _coolDown: number
Focus
Focus

Description:

An internal counter to allow for the browser 'cooling down' after coming back into focus.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 318)
Focus
Focus
<private> _limitRate: number
Focus
Focus

Description:

Internal value holding the fps rate limit in ms.

Type:
number
Since: 3.60.0
Source: src/core/TimeStep.js (Line 154)
Focus
Focus
<private> _min: number
Focus
Focus

Description:

The minimum fps value in ms.

Defaults to 200ms between frames (i.e. super slow!)

Type:
number
Since: 3.0.0
Source: src/core/TimeStep.js (Line 164)
Focus
Focus
<private> _pauseTime: number
Focus
Focus

Description:

The timestamp at which the game became paused, as determined by the Page Visibility API.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 307)
Focus
Focus
<private> _target: number
Focus
Focus

Description:

The target fps value in ms.

Defaults to 16.66ms between frames (i.e. normal)

Type:
number
Since: 3.0.0
Source: src/core/TimeStep.js (Line 176)
Focus
Focus
<readonly> actualFps: number
Focus
Focus

Description:

An exponential moving average of the frames per second.

Type:
number
Default: 60
Since: 3.0.0
Source: src/core/TimeStep.js (Line 188)
Focus
Focus

Description:

A callback to be invoked each time the TimeStep steps.

Type:
Default: NOOP
Since: 3.0.0
Source: src/core/TimeStep.js (Line 223)
Focus
Focus
delta: number
Focus
Focus

Description:

The delta time, in ms, since the last game step. This is a clamped and smoothed average value.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 329)
Focus
Focus
deltaHistory: Array.<number>
Focus
Focus

Description:

Internal array holding the previous delta values, used for delta smoothing.

Type:
Array.<number>
Since: 3.0.0
Source: src/core/TimeStep.js (Line 349)
Focus
Focus
deltaIndex: number
Focus
Focus

Description:

Internal index of the delta history position.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 339)
Focus
Focus
deltaSmoothingMax: number
Focus
Focus

Description:

The maximum number of delta values that are retained in order to calculate a smoothed moving average.

This can be changed in the Game Config via the fps.deltaHistory property. The default is 10.

Type:
number
Default: 10
Since: 3.0.0
Source: src/core/TimeStep.js (Line 358)
Focus
Focus
<readonly> forceSetTimeOut: boolean
Focus
Focus

Description:

You can force the TimeStep to use SetTimeOut instead of Request Animation Frame by setting the forceSetTimeOut property to true in the Game Configuration object. It cannot be changed at run-time.

Type:
boolean
Default: false
Since: 3.0.0
Source: src/core/TimeStep.js (Line 233)
Focus
Focus
fpsLimit: number
Focus
Focus

Description:

Enforce a frame rate limit. This forces how often the Game step will run. By default it is zero, which means it will run at whatever limit the browser (via RequestAnimationFrame) can handle, which is the optimum rate for fast-action or responsive games.

However, if you are building a non-game app, like a graphics generator, or low-intensity game that doesn't require 60fps, then you can lower the step rate via this Game Config value:

fps: {
  limit: 30
}

Setting this beyond the rate of RequestAnimationFrame will make no difference at all.

Use it purely to restrict updates in low-intensity situations only.

Type:
number
Default: 0
Since: 3.60.0
Source: src/core/TimeStep.js (Line 115)
Focus
Focus
<readonly> frame: number
Focus
Focus

Description:

The current frame the game is on. This counter is incremented once every game step, regardless of how much time has passed and is unaffected by delta smoothing.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 282)
Focus
Focus
<readonly> framesThisSecond: number
Focus
Focus

Description:

The number of frames processed this second.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 212)
Focus
Focus
<readonly> game: Phaser.Game
Focus
Focus

Description:

A reference to the Phaser.Game instance.

Type:
Since: 3.0.0
Source: src/core/TimeStep.js (Line 43)
Focus
Focus
hasFpsLimit: boolean
Focus
Focus

Description:

Is the FPS rate limited?

This is set by setting the Game Config limit value to a value above zero.

Consider this property as read-only.

Type:
boolean
Default: false
Since: 3.60.0
Source: src/core/TimeStep.js (Line 140)
Focus
Focus
<readonly> inFocus: boolean
Focus
Focus

Description:

Is the browser currently considered in focus by the Page Visibility API?

This value is set in the blur method, which is called automatically by the Game instance.

Type:
boolean
Default: true
Since: 3.0.0
Source: src/core/TimeStep.js (Line 294)
Focus
Focus
lastTime: number
Focus
Focus

Description:

The time of the previous step.

This is typically a high resolution timer value, as provided by Request Animation Frame.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 270)
Focus
Focus
minFps: number
Focus
Focus

Description:

The minimum fps rate you want the Time Step to run at.

Setting this cannot guarantee the browser runs at this rate, it merely influences the internal timing values to help the Timestep know when it has gone out of sync.

Type:
number
Default: 5
Since: 3.0.0
Source: src/core/TimeStep.js (Line 88)
Focus
Focus
<readonly> nextFpsUpdate: number
Focus
Focus

Description:

The time at which the next fps rate update will take place.

When an fps update happens, the framesThisSecond value is reset.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 199)
Focus
Focus
now: number
Focus
Focus

Description:

The time, set at the start of the current step.

This is typically a high resolution timer value, as provided by Request Animation Frame.

This can differ from the time value in that it isn't calculated based on the delta value.

Type:
number
Default: 0
Since: 3.18.0
Source: src/core/TimeStep.js (Line 396)
Focus
Focus
panicMax: number
Focus
Focus

Description:

The number of frames that the cooldown is set to after the browser panics over the FPS rate, usually as a result of switching tabs and regaining focus.

This can be changed in the Game Config via the fps.panicMax property. The default is 120.

Type:
number
Default: 120
Since: 3.0.0
Source: src/core/TimeStep.js (Line 370)
Focus
Focus

Description:

The Request Animation Frame DOM Event handler.

Type:
Since: 3.0.0
Source: src/core/TimeStep.js (Line 53)
Focus
Focus
rawDelta: number
Focus
Focus

Description:

The actual elapsed time in ms between one update and the next.

Unlike with delta, no smoothing, capping, or averaging is applied to this value. So please be careful when using this value in math calculations.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 383)
Focus
Focus
<readonly> running: boolean
Focus
Focus

Description:

A flag that is set once the TimeStep has started running and toggled when it stops. The difference between this value and started is that running is toggled when the TimeStep is sent to sleep, where-as started remains true, only changing if the TimeStep is actually stopped, not just paused.

Type:
boolean
Default: false
Since: 3.0.0
Source: src/core/TimeStep.js (Line 74)
Focus
Focus
smoothStep: boolean
Focus
Focus

Description:

Apply smoothing to the delta value used within Phasers internal calculations?

This can be changed in the Game Config via the fps.smoothStep property. The default is true.

Smoothing helps settle down the delta values after browser tab switches, or other situations which could cause significant delta spikes or dips. By default it has been enabled in Phaser 3 since the first version, but is now exposed under this property (and the corresponding game config smoothStep value), to allow you to easily disable it, should you require.

Type:
boolean
Since: 3.22.0
Source: src/core/TimeStep.js (Line 410)
Focus
Focus
startTime: number
Focus
Focus

Description:

The time at which the game started running.

This value is adjusted if the game is then paused and resumes.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 258)
Focus
Focus
<readonly> started: boolean
Focus
Focus

Description:

A flag that is set once the TimeStep has started running and toggled when it stops.

Type:
boolean
Default: false
Since: 3.0.0
Source: src/core/TimeStep.js (Line 63)
Focus
Focus
targetFps: number
Focus
Focus

Description:

The target fps rate for the Time Step to run at.

Setting this value will not actually change the speed at which the browser runs, that is beyond the control of Phaser. Instead, it allows you to determine performance issues and if the Time Step is spiraling out of control.

Type:
number
Default: 60
Since: 3.0.0
Source: src/core/TimeStep.js (Line 101)
Focus
Focus
time: number
Focus
Focus

Description:

The time, updated each step by adding the elapsed delta time to the previous value.

This differs from the TimeStep.now value, which is the high resolution time value as provided by Request Animation Frame.

Type:
number
Default: 0
Since: 3.0.0
Source: src/core/TimeStep.js (Line 245)
Focus
Focus

Methods

blur()
Focus
Focus

Description:

Called by the Game instance when the DOM window.onBlur event triggers.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 427)
Focus
Focus
destroy()
Focus
Focus

Description:

Destroys the TimeStep. This will stop Request Animation Frame, stop the step, clear the callbacks and null any objects.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 852)
Focus
Focus
focus()
Focus
Focus

Description:

Called by the Game instance when the DOM window.onFocus event triggers.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 438)
Focus
Focus
getDuration()
Focus
Focus

Description:

Gets the duration which the game has been running, in seconds.

Returns:
Description:

The duration in seconds.

Type:
  • number
Since: 3.17.0
Source: src/core/TimeStep.js (Line 808)
Focus
Focus
getDurationMS()
Focus
Focus

Description:

Gets the duration which the game has been running, in ms.

Returns:
Description:

The duration in ms.

Type:
  • number
Since: 3.17.0
Source: src/core/TimeStep.js (Line 821)
Focus
Focus
pause()
Focus
Focus

Description:

Called when the visibility API says the game is 'hidden' (tab switch out of view, etc)

Since: 3.0.0
Source: src/core/TimeStep.js (Line 451)
Focus
Focus
resetDelta()
Focus
Focus

Description:

Resets the time, lastTime, fps averages and delta history. Called automatically when a browser sleeps them resumes.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 475)
Focus
Focus
resume()
Focus
Focus

Description:

Called when the visibility API says the game is 'visible' again (tab switch back into view, etc)

Since: 3.0.0
Source: src/core/TimeStep.js (Line 462)
Focus
Focus
sleep()
Focus
Focus

Description:

Sends the TimeStep to sleep, stopping Request Animation Frame (or SetTimeout) and toggling the running flag to false.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 755)
Focus
Focus
smoothDelta(delta)
Focus
Focus

Description:

Takes the delta value and smooths it based on the previous frames.

Called automatically as part of the step.

Parameters:

name type description
delta number

The delta value for this step.

Returns:
Description:

The smoothed delta value.

Type:
  • number
Since: 3.60.0
Source: src/core/TimeStep.js (Line 539)
Focus
Focus
start(callback)
Focus
Focus

Description:

Starts the Time Step running, if it is not already doing so. Called automatically by the Game Boot process.

Parameters:

name type description
callback Phaser.Types.Core.TimeStepCallback

The callback to be invoked each time the Time Step steps.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 504)
Focus
Focus
step(time)
Focus
Focus

Description:

The main step method. This is called each time the browser updates, either by Request Animation Frame, or by Set Timeout. It is responsible for calculating the delta values, frame totals, cool down history and more. You generally should never call this method directly.

Parameters:

name type description
time number

The timestamp passed in from RequestAnimationFrame or setTimeout.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 689)
Focus
Focus
stepLimitFPS(time)
Focus
Focus

Description:

The main step method with an fps limiter. This is called each time the browser updates, either by Request Animation Frame, or by Set Timeout. It is responsible for calculating the delta values, frame totals, cool down history and more. You generally should never call this method directly.

Parameters:

name type description
time number

The timestamp passed in from RequestAnimationFrame or setTimeout.

Since: 3.60.0
Source: src/core/TimeStep.js (Line 638)
Focus
Focus
stop()
Focus
Focus

Description:

Stops the TimeStep running.

Returns:
Description:

The TimeStep object.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 834)
Focus
Focus
tick()
Focus
Focus

Description:

Manually calls TimeStep.step.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 735)
Focus
Focus
updateFPS(time)
Focus
Focus

Description:

Update the estimate of the frame rate, fps. Every second, the number of frames that occurred in that second are included in an exponential moving average of all frames per second, with an alpha of 0.25. This means that more recent seconds affect the estimated frame rate more than older seconds.

When a browser window is NOT minimized, but is covered up (i.e. you're using another app which has spawned a window over the top of the browser), then it will start to throttle the raf callback time. It waits for a while, and then starts to drop the frame rate at 1 frame per second until it's down to just over 1fps. So if the game was running at 60fps, and the player opens a new window, then after 60 seconds (+ the 'buffer time') it'll be down to 1fps, so rafin'g at 1Hz.

When they make the game visible again, the frame rate is increased at a rate of approx. 8fps, back up to 60fps (or the max it can obtain)

There is no easy way to determine if this drop in frame rate is because the browser is throttling raf, or because the game is struggling with performance because you're asking it to do too much on the device.

Compute the new exponential moving average with an alpha of 0.25.

Parameters:

name type description
time number

The timestamp passed in from RequestAnimationFrame or setTimeout.

Since: 3.60.0
Source: src/core/TimeStep.js (Line 603)
Focus
Focus
wake([seamless])
Focus
Focus

Description:

Wakes-up the TimeStep, restarting Request Animation Frame (or SetTimeout) and toggling the running flag to true. The seamless argument controls if the wake-up should adjust the start time or not.

Parameters:

name type arguments Default description
seamless boolean <optional> false

Adjust the startTime based on the lastTime values.

Since: 3.0.0
Source: src/core/TimeStep.js (Line 771)
Focus
Focus