Main Content

You are here:

A friendly guide to the Timer Class

Hello and welcome. Today we will be looking at the Timer class, which is an improved version of the setInterval function of AS2. TImers can be useful in many instances:

Imports required

You might want to put these at the top of your document, especially if you put the timer in an external class

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

Creating a Timer

You can create a timer like you would a Movie Clip. However, you use the Timer constructor function to automatically set up the timer. You can do this like so.

var name = new Timer(countdown, [optional number of repeats];

Name is the name you want to use for the timer. In the next examples I will be using myTimer. Countdown refers to the time between "ticks" - this is a time in milliseconds. Number of Repeats is the number of times the timer will tick before the timer stops itself. This defaults to 0 (infinite).

An example is below. This timer will tick every 1000ms (1 second), 10 times before stopping.

var myTimer:Timer = new Timer(1000,10);

Unfortunately we still haven't told the timer what to do every second. For that, we need to add an event listener:

myTimer.addEventListener(TimerEvent.TIMER,someFunction);

someFunction is the function you want to called every whatever ms (it's the event handler). Now we what to create this function, which will be similar to other event listeners but its only argument is not an Event type but a TimerEvent type. It will look something like this:

function someFunction(event:TimerEvent) {
        trace("Tick");
}

Note: You can add as many or as few of these as you like. Unfortunately, we haven't started the timer yet, but don't worry, it's not too difficult.

myTimer.start();

So, if we want to make a clock that ticked every second for a minute we would do it like so:

var myTimer:Timer = new Timer(1000,60);
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
        trace("Tick");
}

Stopping the Timer

If you want to stop the timer, it's quite simple, just put:

myTimer.stop();

Restarting the Timer

If you have deliberately stopped the timer using the code above (the stop() function), then you can simply restart the timer using:

myTimer.start();

However, if your timer has reached its limit in terms of number of repeats, it won't restart. For that, you need to reset the timer, to set its in-built number-of-repeats-so-far counter back to 0, then start the timer again:

myTimer.reset();
                                myTimer.start();

Beware though; the code above does take a few milliseconds to implement, so you might want to just set the number of repeats higher when you make the timer.

Other associated functions and properties

getTimer()
For use inside the timer event function. Will return to you how many milliseconds have passed since the timer started or was reset - is useful for judging the margin of error, as timers are imperfect. Put it inside a trace e.g. trace(getTimer());
delay
Change the delay of a timer, e.g. myTimer.delay = 2000;
repeatCount
Change the number of times the timer will repeat before finishing e.g. myTimer.repeatCount = 60;
currentCount
Read-Only. This holds the number of times the timer has ticked since it was started or reset. e.g. trace(myTimer.currentCount);
running
Read-Only. For use outside the timer event function. This holds a boolean (true/false) value that holds whether the timer is running or not. e.g. trace(myTimer.running);
removeEventListener(Event, Function)
er, removes an Event Listener. For more info, visit the events in AS3 tutorial. e.g. myTimer.removeEventListener(TimerEvent.EVENT,someFunction);

Useful Examples

60 Second Countdown ~

var myTimer:Timer = new Timer(300,60);
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
        trace(String((60-myTimer.currentCount)));
        if (myTimer.currentCount == 60) {
                myTimer.reset();
                myTimer.start();
        }
}

Random Interval Timer (by request) ~

var myTimer:Timer = new Timer(1000,0);
var lastCall:uint = 0;
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
        trace("Time since last call: " + String(getTimer() - lastCall));//or do something else
        myTimer.delay = Math.random()*3000;
        lastCall = getTimer();
}

Hopefully you should now have an improved understanding the Timer class in AS3. Well done!

Harry.

Comments