Search code examples
actionscript-3eventsairtimerflash-cs6

TimerEvent function ignoring Timer delay


I'm working in AS3, Flash AIR 3.2 for iOS SDK. I'm trying to run part of the program only after myLoader finishes loading an image. I have a myTimer.start(); which runs inside myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);.

What seems to be the problem at the moment is that the program is ignoring the 1000ms. The program is running after myLoader is finished at the moment, but it just seems to be doing its own thing in terms of the delay.

EDIT: Being more precise here... The program seems to be ignoring the Timer delay. Even if Timer is set to 100000ms. It seems to be running the rest of the program right after the image is loaded.

EDIT: I still had my methods running inside my Main() as well as the timerListener() in the code. Thought I commented them out. Whoops!

var myTimer:Timer = new Timer(1000);

public function Main()
    {
        init();
        displayImage();

        myTimer.addEventListener(TimerEvent.TIMER, timerListener);
        myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);

    }

public function displayImage():void {

        myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);

        myLoader.load(fileRequest);
    }

public function onLoaderComplete(e:Event) {
        //start Timer event here
        myTimer.start();
    }

public function timerListener (e:TimerEvent):void{
        trace("Timer is Triggered");
        myTimer.stop();

        aMethod();
        anotherMethod();
        moreMethods();
    }

Solution

  • You don't really clarify what you mean by "the timer is doing its own thing." Is the timer shorter or longer than you expect?

    What I think is likely going on here is that your timer tick and your frame rate are out of synch. If you're familiar with the concept of the elastic racetrack, you know that the single-threaded nature of Flash (unless you're using worker threads) means that the screen can't update during a script and vice-versa. This means that if your Timer fires while the Display list is updating, it just has to wait until the display list finishes and may even have to wait until other scripts have run, depending on how Flash is prioritizing the different things in its queue.

    From the Timer API:

    Depending on the SWF file's framerate or the runtime environment (available memory and other factors), the runtime may dispatch events at slightly offset intervals. For example, if a SWF file is set to play at 10 frames per second (fps), which is 100 millisecond intervals, but your timer is set to fire an event at 80 milliseconds, the event will be dispatched close to the 100 millisecond interval. Memory-intensive scripts may also offset the events.