User Guide

202 Working with Dates and Times
Here is a small sample application showing the Timer class in action:
package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class ShortTimer extends Sprite
{
public function ShortTimer()
{
// creates a new five-second Timer
var minuteTimer:Timer = new Timer(1000, 5);
// designates listeners for the interval and completion events
minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE,
onTimerComplete);
// starts the timer ticking
minuteTimer.start();
}
public function onTick(evt:TimerEvent):void
{
// displays the tick count so far
// The target of this event is the Timer instance itself.
trace("tick " + evt.target.currentCount);
}
public function onTimerComplete(evt:TimerEvent):void
{
trace("Time's Up!");
}
}
}
When the ShortTimer class is created, it creates a Timer instance that will tick once per
second for five seconds. Then it adds two listeners to the timer: one that listens to each tick,
and one that listens for the
timerComplete event.
Next, it starts the timer ticking, and from that point forward, the
onTick() method executes
at one-second intervals.
The
onTick() method simply displays the current tick count. After five seconds have passed,
the
onTimerComplete() method executes, telling you that the time is up.