Timer (flash.utils.Timer)
The Timer is a utility class in the flash.utils package.
A timer's job is to fire off events at specified intervals.
A small list of potential Timer uses:
- Fire off an event when 5 minutes has passed (e.g. to limit the amount of time a quiz can take)
- Remind a user to do something every hour (e.g. to auto-save the application state)
The below ActionScript shows how to create a Timer instance and start it.
import flash.utils.Timer; import flash.events.TimerEvent; // create a timer which fires every second (1000 ms) var timer:Timer = new Timer( 1000 ); // start the timer timer.start();
The Timer constructor's arguments, delay, specifies how often the
timer will fire timer events.
A timer will fire of TimerEvents named "timer" (TimerEvent.TIMER
at intervals specified in the constructor.
Of course a Timer firing off events is of little use unless there are listeners
for the events.
Listeners are registered by calling timer.addEventListener().
import flash.events.TimerEvent; // add a listener to the timer timer.addEventListener( TimerEvent.TIMER, handleTimerEvent );
Timer Properties, Methods, and Events
Commonly used properties:
- delay:Number - the delay (in milliseconds) between timer events
- running:Boolean - the state of the timer, true if it is running, false if it is stopped
- repeatCount:Number - the number of intervals the timer should run
- currentCount:Number - the number of intervals reached
Commonly used functions:
- Timer( delay:Number, repeatCount:int=0 ) - constructor
- delay - the number of milliseconds to wait before firing events
- repeatCount (optional) - the number of times to fire
- A repeatCount of 0 (the default value) means fire for the life of the application.
- addEventListener( type:String, handler:Function ) - register event listeners
- type - the event type (e.g. "timer" or "timerComplete")
- handler - the function to call when an event occurs
- start() - start the timer
- stop() - stop the timer
Commonly used events:
- TimerEvent.TIMER ("timer") - dispatched at each time interval specified by the
delayproperty - TimerEvent.TIMER_COMPLETE ("timerComplete") - dispatched if and when the the interval count specified by the repeatCount property is reached
Repeating
While some timers are meant to run for the life of the application, they can also be initialized to fire a fixed number of times and then stop.
A second optional constructor argument, repeatCount, specifies how many times an event will fire before the timer stops.
// create a timer which fires every second, five times var timer:Timer = new Timer( 1000, 5 );
Because a repeatCount was specified, the timer will also fire off a "timerComplete"
(TimerEvent.TIMER_COMPLETE) event when it is done repeating.
The default value of 0 for repeatCount means the timer will continue firing until explicitly stopped.
Timer Examples
Simple Example
Below is a full example of Timer in action that uses trace() to print messages in debug mode.
Run the application in debug mode to see the timer events being fired.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <![CDATA[ import flash.utils.Timer; import flash.events.TimerEvent; // create a timer that fires every second, ten times public var timer:Timer = new Timer( 1000, 10 ); public function init():void { // add event listeners timer.addEventListener( TimerEvent.TIMER, handleTimerEvent ); timer.addEventListener( TimerEvent.TIMER_COMPLETE, handleTimerEvent ); // start the timer timer.start(); } public function handleTimerEvent( event:TimerEvent ):void { trace( "Timer Event: " + event ); } ]]> </mx:Script> </mx:Application>
Timer linked to ProgressBar
The following example code shows a Timer powering a ProgressBar.
ProgressBar and Timer Flex Example
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.utils.Timer; import flash.events.TimerEvent; public var timer:Timer = new Timer( 1000 ); public var count:Number = 0; public function init():void { this.timer.addEventListener( TimerEvent.TIMER, updateProgress ); this.timer.start(); } protected function updateProgress( timerEvent:TimerEvent ):void { count++; this.progress.setProgress( (count % 10), 100 ); this.progress.label = (count % 10) + "%"; } ]]> </mx:Script> <mx:ProgressBar id="progress" minimum="100" maximum="100" mode="manual" label="0%" labelPlacement="bottom"> </mx:ProgressBar> </mx:Application>

