views:

31

answers:

1

Hi

Can we use 2 timer events in a single application. I'm trying to use 2 timer events in a single application but the 2nd timer event is not working.... any one have an idea??? how to use 2 timer events in a single application....

Thanks in advance....

+3  A: 

You can have as many numbers of timers as you want in your application. Just initialize them as if you'd initialize any other timer. Post the code you tried and we might be able to fix the issue.

var t1:Timer = new Timer(1000, 0);
t1.addEventListener(TimerEvent.TIMER, timerHandler1);
t1.start();

var t2:Timer = new Timer(500, 0);
t2.addEventListener(TimerEvent.TIMER, timerHandler2);
t2.start();

public function timerHandler1(event:TimerEvent):void {
    trace("First timer triggered");
}
public function timerHandler2(event:TimerEvent):void {
    trace("Second timer triggered");
}

It is not mandatory to use separate listeners; you can as well do:

public var t1:Timer;
public var t2:Timer;

t1 = new Timer(1000, 0);
t2 = new Timer(200, 0);

t1.addEventListener(TimerEvent.TIMER, timerHandler);
t2.addEventListener(TimerEvent.TIMER, timerHandler);

public function timerHandler2(event:TimerEvent):void {
    if(event.target == t1)
        trace("first timer");
    else
        trace("second timer");
}
Amarghosh
+1 Good answer!
Wade Mueller
Thank you Amarghosh, got good information.... Its working also....