Chapter 18. Using Timers


In this chapter

  • Understanding the new Timer class for time‐based events

  • Creating a timer and adding event listeners

  • Accessing the properties of a timer

  • Delaying the execution of a function

  • Creating a clock that uses multiple event listeners on a single timer

The timer is an important and valuable tool used for triggering time‐based events in AS3. Nearly any application that involves real‐world time will use a timer. Timers can be useful for delaying an action, timing out when an asynchronous action takes too long to execute, triggering an action repeatedly or after a delay, or synchronizing several actions.

If you're familiar with ActionScript 2.0, you've probably used the setInterval() function. In ActionScript 3.0, setInterval() has been replaced with the Timer class. Instead of relying on a single function being fired after a set interval, AS3 timers use the new event framework.

Each instance of a timer will repeatedly fire events spaced out by a set amount of time using the AS3 event framework. Using events rather than callback functions not only makes them more consistent and easy to use, but also allows you to trigger multiple functions with a single timer (or with multiple timers).

Timers are similar in some ways to for loops in that both structures can be used to repeat a bit of code several times, but are otherwise quite different. for loops attempt to execute every iteration immediately using as much processing power as needed. No other code ...

Get ActionScript™ 3.0 Bible now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.