It is absolutely sure that you could not catch the time. It alway flow and can not be called back. But Actionscript 3 provides you to use its API to “catch” a period of time to make your Adobe Flex or Adobe AIR application has a good action. So please welcome, Timer and TimerEvent.
What are Timer and TimerEvent suppose to be used?
OK, I assume that you know the basic principle of Actionscript 3 already, so I will focus on how to use it. The Timer class ,which you are going to use:
- It has ability to count “Time” in human’s measure, such as second or minute.
- It will dispatch event one by one while it is counting.
- If you limit the Timer counting, it will dispatch last event at the last count.
- You can force Timer to stop count for a while.
Using Timer
The way you are going to use Timer is same as other Actionscript 3 API, you have to instantiate it first.
timer = new Timer(1000,5); timer.start();
- delay: the time interval for each count. For example, the timer above has set delay as 1,000 millisecond (1 second). So each count will use 1 second.
- repeatCount: amount of time before timer will stop. This looks like more ‘multiply’ function. From the example above, my timer will count 5 time of 1 seconds. That means my timer will count 5 seconds and then stop the counting.
After instantiated, you have to use start() method to start the timer. You can also use stop() method to pause Timer for a while.
/* method stop() will be used to stop Timer for a while. You can continue the counting by use start() again */ timer.stop();
Using TimerEvent
Even you start the timer. But you have to know when each delay has been counted and when the timer finished the couting! At this point, you have to add listener for 2 Timer’s event. Described below:
timer.addEventListener(TimerEvent.TIMER, callback ); timer.addEventListener(TimerEvent.TIMER_COMPLETE, callback);
TimerEvent.TIMER
This event will be dispatch every 1 delay has been counted. For example, I will get this event every 1 second for my Timer above.
TimerEvent.TIMER_COMPLETE
This event will be dispatched out from Timer for you, if you set repeatedCount more than 0. For example, this event will be dispatch when my Timer above finishes the fifth count.
Where to go from here?
How about it? You can use Timer and its event to create a passive action in your Adobe Flex and Adobe AIR application right now. You can try to play around with your new toys with simple project in Flash Builder. If you are still interesting in more of my Actionscript 3 article, please feel free to click the button below to browse of them.
At the point, Thank you and have a great day.
