User Guide

Controlling time intervals 201
Controlling time intervals
When you develop applications using the Flash authoring tool, you have access to the
timeline, which provides a steady, frame-by-frame progression through your application. In
pure ActionScript projects, however, you must rely on other timing mechanisms.
Loops versus timers
In some programming languages, you must devise your own timing schemes using loop
statements like
for or do..while.
Loop statements generally execute as fast as the local machine allows, which means that the
application runs faster on some machines and slower on others. If your application needs a
consistent timing interval, you need to tie it to an actual calendar or clock time. Many
applications, such as games, animations, and real-time controllers, need regular, time-driven
ticking mechanisms that are consistent from machine to machine.
The ActionScript 3.0 Timer class provides a powerful solution. Using the ActionScript 3.0
event model, the Timer class dispatches timer events whenever a specified time interval is
reached.
The Timer class
The preferred way to handle timing functions in ActionScript 3.0 is to use the Timer class
(flash.utils.Timer), which can be used to dispatch events whenever an interval is reached.
To start a timer, you first create an instance of the Timer class, telling it how often to generate
a timer event and how many times to do so before stopping.
For example, the following code creates a Timer instance that dispatches an event every
second and continues for 60 seconds:
var oneMinuteTimer:Timer = new Timer(1000, 60);
The Timer object dispatches a TimerEvent object each time the given interval is reached. A
TimerEvent objects event type is
timer (defined by the constant TimerEvent.TIMER). A
TimerEvent object contains the same properties as a standard Event object.
If the Timer instance is set to a fixed number of intervals, it will also dispatch a
timerComplete event (defined by the constant TimerEvent.TIMER_COMPLETE) when it
reaches the final interval.