Passing State with a Timer

Early on in my Flex work I found many cases where I needed the function after a timer event to have some more information. In other words, I really needed to pass state of variables to the next function. You could do this by using global variables, but a cleaner way is to create a subclass of the Timer class and pass along an object.

To do this, you can create this actionscript class in your main mxml directory (or get fancy and name it within subdirectories) as ”” and the contents are simply:

// ActionScript file
// ObjTimer is just a timer that can hold an object of variables to pass along
package {
    import flash.utils.Timer;
    public class ObjTimer extends Timer {
        public var obj:Object = new Object;
        public function ObjTimer(delay:Number, repeatCount:int = 0) {
             super(delay, repeatCount);

and then you can access it from within your mxml like so for example:

var newTimer: ObjTimer = new ObjTimer(900,1);
newTimer.obj["myvar1"] = "test";
newTimer.obj["firstname"] = "joe";
newTimer.obj["count"] = 10;
newTimer.addEventListener(TimerEvent.TIMER, handleFunction);

private function handleFunction(e:Event):void{
    var value:String =["myvar1"];
    var fn:String =["firstname"];
    var count:int = parseInt(["count"]);

This example is really quick-n-dirty, you can make it nicer by adding setters and getters in the new class, but for an easy implementation this should get you going.

1 reply on “Passing State with a Timer”

You can also extend Timer and make it dynamic:
public dynamic class DynoTimer extends Timer { …}

and then use:
var value:String =[“myvar1”];

Leave a Reply

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