The Flash. util. Timer class allows you to call a method by adding a time event or delay. Create an Instance Object using the timer constructor and pass a millisecond number as the construction parameter as the interval. The following example instantiate a timer object to send an event signal every one second:
var timer.Timer = new Timer(1000);
Once a timer instance is created, an event listener must be added in the next step to process the event. The timer object sends a falsh. event. timerevent event, which is scheduled Based on the set interval or delay time. The following code defines an event listener and calls the ontimer () method as a processing function:
timer.addEventListener(TimerEvent.TIMER,onTimer);
private function onTimer(event:TimerEvent):void{ trace("on timer"); }
The timer object does not start automatically. You must call the START () method to start it:
timer.start();
By default, only the stop () method is called to stop. However, another method is to pass the second parameter to the constructor as the number of running times. The default value is 0, which is unlimited, the following example sets the timer to run five times:
var timer:Timer = new Timer(1000, 5);
The following code sets the timer latency of 5 seconds to execute the deferredmethod () method:
var timer:Timer = new Timer(5000, 1); timer.addEventListener(TimerEvent.TIMER, deferredMethod); timer.start();