ActionScript 3 simple timer, sample code.

The code below gives you an example of the wonderful timer class, before this timers were a total pain in ActionScript 2, this new class gives us all the functionality we always had to code around before.

———– code sample

var myTimer:Timer = new Timer(5000,1);

myTimer.addEventListener(TimerEvent.TIMER, timerFunction);

function timerFunction(event:TimerEvent):void
{
trace(“Timer function executed”);
}

myTimer.start();

———– end code sample

We first create an instance of the Timer class and tell it we want it to fire every 5 seconds (5000) and to do it only 1 time. Then we create an EventListener and tell it to call our method/function called timerFunction which simply traces some text to the output window. Then fiinally we just need to tell our timer instance to start. The timer instance will stop after it has executed the specified amount of times (in this case once).

Published by Peter Witham

Web applications developer & designer. Certified Flash Designer.

6 thoughts on “ActionScript 3 simple timer, sample code.

  1. TIMER needs to be capitalized otherwise you will get one of those horribly useless error messages from the 2bit compiler.

    myTimer.addEventListener(TimerEvent.Timer, timerFunction);

    should be:

    myTimer.addEventListener(TimerEvent.TIMER, timerFunction);

    Also, if you copy and past, retype the quotation marksin the trace statement.

    Thanks for the posting, a great example!

  2. Good catch on the TIMER thanks, seems there is some character alterations going on between my copy and paste into my blog poster on the quotations.

  3. how can you flash the message exactly 200 ms?
    Is there a method to test whether it is displayed exactly 200 ms?
    By the way, great posting!

  4. Thanks for you fast reply.
    I want to flash a word for a period of exactly 200 ms on the screen. It is for scientific purposes, therefore I can not be more lenient.
    I assume that 200 ms is not a problem for modern screens. Still I would like to find a method to test whether the duration that the stimulus was visible, was exactly 200 ms.

  5. Hmmm,

    Well you can simply change the time count for 200 milliseconds but I am wondering if you can rely of the Flash player to be that accurate. I have to say that I have never considered the Flash player to be that accurate, however if anyone can help please post and let us know. I’ll see if I can find more answers for you.

Leave a comment