pho
pho

Reputation: 25489

Flex Timer issue

The Timer in my class does not seem to fire any TIMER events at all when the interval is more than 5 seconds or after it has measured 5 seconds. I need to measure 30 seconds.

Here's my code

//class ctor
public function myClass() {
    tmr=new Timer(5000, 6);
    tmr.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
    tmr.addEventListener(TimerEvent.TIMER, timerTrace);
}
private function timerComplete(e:TimerEvent):void {
    trace("complete");
}
private function timerTrace(e:TimerEvent):void {
    trace("tick|" + tmr.currentCount);
}

The output I get is

tick|1

When I change the interval to 1000ms and the repeatCount to 30, I get

tick|1
tick|2
tick|3
tick|4

When interval is 30000 and repeatCount is 1, I get no output

The timer never completes.

I tried using setTimeout, but the timeout of 30 seconds doesn't work there either.

How can I add a timeout of 30 seconds?

EDIT

//declare timer
public var tmr as Timer;

//external class
nyClassInstance.tmr.start();

Upvotes: 0

Views: 1337

Answers (1)

Mattias
Mattias

Reputation: 3907

If you only want to set the delay to 30 seconds you should do it like this:

_timer = new Timer(30 * 1000, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
_timer.start();

Example: http://wonderfl.net/c/4duo/

package
{
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.utils.getTimer;

    public class FlashTest extends Sprite
    {
        private var _timer : Timer;
        private var _lastUpdate : int;
        private var _debugText : TextField;

        public function FlashTest()
        {
            _debugText = new TextField();
            addChild(_debugText);

            _lastUpdate = getTimer();

            _timer = new Timer(6 * 1000, 6);
            _timer.addEventListener(TimerEvent.TIMER, onTimerUpdate);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
            _timer.start();

            _debugText.appendText("STARTED");
        }

        private function onTimerUpdate(event : TimerEvent) : void
        {
            _debugText.appendText("\n" + (getTimer() - _lastUpdate) + " - UPDATE " + _timer.currentCount);

            _lastUpdate = getTimer();
        }

        private function onTimerComplete(event : TimerEvent) : void
        {
            _debugText.appendText("\nCOMPLETE");
        }

    }
}

Upvotes: 2

Related Questions