API Documentation for: 0.6.1
Show:

Ticker Class

Defined in: Ticker:35
Module: EaselJS

The Ticker 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. The Ticker class uses a static interface (ex. Ticker.getPaused()) and should not be instantiated.

Example

 createjs.Ticker.addEventListener("tick", handleTick);
 function handleTick(event) {
     // Actions carried out each frame
     if (!event.paused) {
         // Actions carried out when the Ticker is not paused.
     }
 }

To update a stage every tick, the Stage instance can also be used as a listener, as it will automatically update when it receives a tick event:

 createjs.Ticker.addEventListener("tick", stage);

Methods

_getTime

() protected

Defined in _getTime:494

_handleAF

() protected

Defined in _handleAF:417

_handleTimeout

() protected

Defined in _handleTimeout:430

_setupTick

() protected

Defined in _setupTick:440

_tick

() protected

Defined in _tick:457

addEventListener

(
  • type
  • listener
)
Function | Object

Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.

Example

 displayObject.addEventListener("click", handleClick);
 function handleClick(event) {
    // Click happened.
 }

Parameters:

  • type String

    The string type of the event.

  • listener Function | Object

    An object with a handleEvent method, or a function that will be called when the event is dispatched.

Returns:

Function | Object: Returns the listener for chaining or assignment.

addListener

(
  • o
  • pauseable
)
deprecated static

Defined in addListener:215

Deprecated: In favour of the "tick" event. Will be removed in a future version. Use "addEventListener" instead.

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.

dispatchEvent

(
  • eventObj
  • [target]
)
Boolean

Dispatches the specified event to all listeners.

Example

 // Use a string event
 this.dispatchEvent("complete");

 // Use an object
 var event = {
     type: "complete",
     foo: "bar"
 };
 this.dispatchEvent(event);

Parameters:

  • eventObj Object | String

    An object with a "type" property, or a string type. If a string is used, dispatchEvent will construct a generic event object with the specified type.

  • [target] Object optional

    The object to use as the target property of the event object. This will default to the dispatching object.

Returns:

Boolean: Returns true if any listener returned true.

getFPS

() Number static

Defined in getFPS:316

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.

getInterval

() Number static

Defined in getInterval:295

Returns the current target time between ticks, as set with setInterval.

Returns:

Number: The current target interval in milliseconds between tick events.

getMeasuredFPS

(
  • [ticks]
)
Number static

Defined in getMeasuredFPS:327

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.

getTicks

(
  • pauseable
)
Number static

Defined in getTicks:402

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 true only tick events broadcast while Ticker is not paused will be returned. If false, 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.

getTime

(
  • [runTime=false]
)
Number static

Defined in getTime:389

Returns the number of milliseconds that have elapsed since Ticker was initialized. For example, you could use this in a time synchronized animation to determine the exact amount of time that has elapsed.

Parameters:

  • [runTime=false] Boolean optional

    If true only time elapsed while Ticker was not paused will be returned. If false, 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 initialized.

hasEventListener

(
  • type
)
Boolean

Indicates whether there is at least one listener for the specified event type.

Parameters:

  • type String

    The string type of the event.

Returns:

Boolean: Returns true if there is at least one listener for the specified event.

init

() static

Defined in init:235

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.

initialize

() protected

Inherited from EventDispatcher: initialize:98

Initialization method.

removeAllEventListeners

(
  • [type]
)

Removes all listeners for the specified type, or all listeners of all types.

Example

 // Remove all listeners
 displayObject.removeAllEvenListeners();

 // Remove all click listeners
 displayObject.removeAllEventListeners("click");

Parameters:

  • [type] String optional

    The string type of the event. If omitted, all listeners for all types will be removed.

removeAllListeners

() deprecated static

Defined in removeAllListeners:269

Deprecated: In favour of the "tick" event. Will be removed in a future version. Use "removeAllEventListeners" instead.

Removes all listeners.

removeEventListener

(
  • type
  • listener
)

Removes the specified event listener.

Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.

Example

 displayObject.removeEventListener("click", handleClick);

Parameters:

  • type String

    The string type of the event.

  • listener Function | Object

    The listener function or object.

removeListener

(
  • o
)
deprecated static

Defined in removeListener:251

Deprecated: In favour of the "tick" event. Will be removed in a future version. Use "removeEventListener" instead.

Removes the specified listener.

Parameters:

  • o Object

    The object or function to remove from listening from the tick event.

setFPS

(
  • value
)
static

Defined in setFPS:305

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.

setInterval

(
  • interval
)
static

Defined in setInterval:281

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.

setPaused

(
  • value
)
static

Defined in setPaused:345

Changes the "paused" state of the Ticker, which can be retrieved by the Ticker/getPaused method, and is passed as the "paused" property of the tick event. When the ticker is paused, all listeners will still receive a tick event, but the paused property will be false.

Note that in EaselJS v0.5.0 and earlier, "pauseable" listeners would not receive the tick callback when Ticker was paused. This is no longer the case.

Example

 createjs.Ticker.addEventListener("tick", handleTick);
 createjs.Ticker.setPaused(true);
 function handleTick(event) {
     console.log("Paused:", event.paused, createjs.Ticker.getPaused());
 }

Parameters:

  • value Boolean

    Indicates whether to pause (true) or unpause (false) Ticker.

toString

() String

Inherited from EventDispatcher: toString:231

Returns:

String: a string representation of the instance.

Properties

_inited

Boolean protected

Defined in _inited:134

_interval

Number protected

Defined in _interval:171

_lastTime

Number protected

Defined in _lastTime:178

_listeners

Array protected

Inherited from EventDispatcher but overwritten in _listeners:113

_pauseable

Array protected

Defined in _pauseable:120

_paused

Boolean protected

Defined in _paused:127

_pausedTicks

Number protected

Defined in _pausedTicks:163

The number of ticks that have passed while Ticker has been paused

_pausedTime

Number protected

Defined in _pausedTime:148

_rafActive

Boolean protected

Defined in _rafActive:199

_startTime

Number protected

Defined in _startTime:141

_ticks

Number protected

Defined in _ticks:155

The number of ticks that have passed

_tickTimes

Array protected

Defined in _tickTimes:192

_timeoutID

Number protected

Defined in _timeoutID:206

_times

Array protected

Defined in _times:185

useRAF

Boolean static

Defined in useRAF:89

Indicates whether Ticker should use requestAnimationFrame if it is supported in the browser. If false, Ticker will use setTimeout. If you use RAF, it is recommended that you set the framerate to a divisor of 60 (ex. 15, 20, 30, 60).

Default: false

Events

tick

Defined in tick:67

Available since 0.6.0

Dispatched each tick. The event will be dispatched to each listener even when the Ticker has been paused using setPaused.

Example

 createjs.Ticker.addEventListener("tick", handleTick);
 function handleTick(event) {
     console.log("Paused:", event.paused, event.delta);
 }

Event Payload:

  • target Object

    The object that dispatched the event.

  • type String

    The event type.

  • paused Boolean

    Indicates whether the ticker is currently paused.

  • delta Number

    The time elapsed in ms since the last tick.

  • time Number

    The total time in ms since Ticker was initialized.

  • runTime Number

    The total time in ms that Ticker was not paused since it was initialized. For example, you could determine the amount of time that the Ticker has been paused since initialization with time-runTime.