How to use Timer class instead of setInterval()

In ActionScript 3.0, the preferred way to execute actions at specific time intervals is the use of the new Timer class. In ActionScript 2.0, we had the setInterval() function and this was the way to use it:
– write a function that would be called every time the time interval passed the limit
– call the setInterval() function that would receive as parameters the object that has the target function defined (not always necessary), the name of the function (or a reference to it), the time interval at which to call the target function and the list of parameters for the target function (also not necessary)
– to stop the time interval there should have also been a variable defined to hold the interval id and then call clearInterval() with the interval’s id as parameter.

AS2 example – call a function 10 times

For example, the next piece of code will call timeHandler 10 times and then interrupt the repeated calls of the function:

var counter:Number = 0;
var intervalID:Number;

function timerHandler(param:String):Void {
counter++;
trace(“This function has been called “+counter+” times with the \””+param+”\” parameter”);
if (counter == 10) clearInterval(intervalID);
}

intervalID = setInterval(timerHandler, 1000, “test”);

AS3 example

For AS3, the steps are similar but setInterval() was replaced by the Timer class. Here is the above code translated into AS3:

import flash.utils.Timer;
import flash.events.TimerEvent;

var counter:int = 0;
var timerObject:Timer = new Timer(1000, 10);
timerObject.addEventListener(TimerEvent.TIMER, timerHandler);
timerObject.start();

function timerHandler(eventObject:TimerEvent):void {
counter++;
trace(“This function has been called “+counter+” times”);
}

AS3 code explained

You can see that when creating a new instance of the Timer class, the constructor has two parameters passed to it: the first is the time interval (in milliseconds) and the second is the number of repetitions, that is the number of times to call the handler function. If the second parameter is not given or it’s 0, then the timer repeats infinitely. Next, you need to define the handler function for the TimerEvent.TIMER event, which gets dispatched every time the time interval is passed. The Timer class also dispatches another event, TimerEvent.TIMER_COMPLETE. This event is dispatched only when the timer object has finished the number of repetitions. If the repeatCount, the second parameter of the Timer constructor is not defined or it’s 0, then the TIMER_COMPLETE event will never get called. Once you create the Timer instance, you need to start it (timerObject.start()) otherwise it won’t have any effect.

As you can see, for the AS3 version, you cannot pass any parameters directly to the timer event handler functions. If you want to get the same result as the AS2 example, then you need to create another function that would be called by the timer event handler and get the parameter passed to this second function:

function timerHandler(eventObject:TimerEvent):void {
callback(“test”);
}

function callback(param:String):void {
counter++;
trace(“This function has been called “+counter+” times with the \””+param+”\” parameter”);
}

The Timer class

The Timer object is more complex and offers us more information on the current interval calls. We can have access to the number of repetitions the Timer object has reached (timerObject.currentCount), the total number of repetitions (timerObject.repeatCount), the time interval (timerObject.delay) and we can even find out if the Timer object is running or not (timerObject.running).

By using the Timer class, you also have the ability to stop the repetitions (timerObject.stop()) and then resume it, if you call the start() method again. It even has the possibility to reset the counter (timerOject.reset()) and the Timer object will start the repetitions all over again.

Get the number of repetitions from the Timer object

As mentioned previously, you can access different parameters of the Timer object and you can do it even in the timer event handler function. The next example displays the number of times the function has been called, without having to define an extra variable for that:

import flash.utils.Timer;
import flash.events.TimerEvent;

var timerObject:Timer = new Timer(1000, 10);
timerObject.addEventListener(TimerEvent.TIMER, timerHandler);
timerObject.start();

function timerHandler(eventObject:TimerEvent):void {
trace(eventObject.target.currentCount+” repetitions out of “+eventObject.target.repeatCount);
}

As you can see, the Timer class is a welcome addition to the ActionScript 3 framework. Enjoy it.

8 thoughts on “How to use Timer class instead of setInterval()

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>