static
Class Ticker
The Ticker class uses a static interface (ex. Ticker.getPaused()) and should not be instantiated.
Provides a centralized tick or heartbeat broadcast at a set interval. Listeners can subscribe
to the tick event to be notified when a set time interval has elapsed.
Note that the interval that the tick event is called is a target interval, and may be broadcast
at a slower interval during times of high CPU load.
Properties
Number of ticks that have passed while Ticker has been paused
_ticks
- protected Number
Number of ticks that have passed
_times
- protected Array[Number]
Specifies the animation target to use with requestAnimationFrame if useRAF is true.
useRAF
- static Boolean
Indicates whether Ticker should use requestAnimationFrame if it is supported in the browser. If false, Ticker
will use setTimeout. If you change this value, you must call setInterval or setFPS to reinitialize the Ticker.
Methods
protected
void
_getTime
(
)
protected
void
_handleAF
(
)
protected
void
_handleTimeout
(
)
protected
void
_setupTick
(
)
static
void
addListener
(
o
,
pauseable
)
Adds a listener for the tick event. The listener must be either an object exposing a .tick() method,
or a function. The listener will be called once each tick / interval. The interval is specified via the
.setInterval(ms) method.
The tick method or function is passed two parameters: the elapsed time between the
previous tick and the current one, and a boolean indicating whether Ticker is paused.
- Parameters:
-
o
<Object>
The object or function to add as a listener.
-
pauseable
<Boolean>
If false, the listener will continue to have tick called
even when Ticker is paused via Ticker.pause(). Default is true.
- Returns:
void
static
Number
getFPS
(
)
Returns the target frame rate in frames per second (FPS). For example, with an
interval of 40, getFPS() will return 25 (1000ms per second divided by 40 ms per tick = 25fps).
- Returns:
Number
- The current target number of frames / ticks broadcast per second.
static
Number
getInterval
(
)
Returns the current target time between ticks, as set with setInterval.
- Returns:
Number
- The current target interval in milliseconds between tick events.
static
Number
getMeasuredFPS
(
ticks
)
Returns the actual frames / ticks per second.
- Parameters:
-
ticks
<Number>
Optional. The number of previous ticks over which to measure the actual
frames / ticks per second. Defaults to the number of ticks per second.
- Returns:
Number
- The actual frames / ticks per second. Depending on performance, this may differ
from the target frames per second.
static
Boolean
getPaused
(
)
Returns a boolean indicating whether Ticker is currently paused, as set with setPaused.
- Returns:
Boolean
- Whether the Ticker is currently paused.
static
Number
getTicks
(
pauseable
)
Returns the number of ticks that have been broadcast by Ticker.
- Parameters:
-
pauseable
<Boolean>
Indicates whether to include ticks that would have been broadcast
while Ticker was paused. If false only tick events broadcast while Ticker is not paused will be returned.
If true, tick events that would have been broadcast while Ticker was paused will be included in the return
value. The default value is false.
- Returns:
Number
- of ticks that have been broadcast.
static
Number
getTime
(
pauseable
)
Returns the number of milliseconds that have elapsed since the first tick event listener was added to
Ticker. For example, you could use this in a time synchronized animation to determine the exact amount of
time that has elapsed.
- Parameters:
-
pauseable
<Boolean>
Indicates whether to include time elapsed
while Ticker was paused. If false only time elapsed while Ticker is not paused will be returned.
If true, the value returned will be total time elapsed since the first tick event listener was added.
- Returns:
Number
- Number of milliseconds that have elapsed since Ticker was begun.
static
void
init
(
)
Initializes or resets the timer, clearing all associated listeners and fps measuring data, starting the tick.
This is called automatically when the first listener is added.
static
void
removeAllListeners
(
)
Removes all listeners.
static
void
removeListener
(
o
)
Removes the specified listener.
- Parameters:
-
o
<Object>
The object or function to remove from listening from the tick event.
- Returns:
void
static
void
setFPS
(
value
)
Sets the target frame rate in frames per second (FPS). For example, with an interval of 40, getFPS() will
return 25 (1000ms per second divided by 40 ms per tick = 25fps).
- Parameters:
-
value
<Number>
Target number of ticks broadcast per second.
- Returns:
void
static
void
setInterval
(
interval
)
Sets the target time (in milliseconds) between ticks. Default is 50 (20 FPS).
Note actual time between ticks may be more than requested depending on CPU load.
- Parameters:
-
interval
<Number>
Time in milliseconds between ticks. Default value is 50.
- Returns:
void
static
void
setPaused
(
value
)
While Ticker is paused, pausable listeners are not ticked. See addListener for more information.
- Parameters:
-
value
<Boolean>
Indicates whether to pause (true) or unpause (false) Ticker.
- Returns:
void
Events
tick
(
timeElapsed
)
Event broadcast once each tick / interval. The interval is specified via the
.setInterval(ms) or setFPS methods.
- Parameters:
-
timeElapsed
<Number>
The time elapsed in milliseconds since the last tick event.