The docs for flash.utils.setTimeout() state:
Instead of using this method, consider creating a Timer object, with the specified interval, using 1 as the repeatCount parameter (which sets the timer to run only once).
Does anyone know if there is a (significant) advantage in doing so? Using setTimeout is a lot easier when you only need to delay 1 call.
setTimeout
actually uses a Timer
subclass, the SetIntervalTimer
, which is an internal class. You can check by doing setTimeout(function ():void { throw "booom"; }, 1);
. You'll see it in the stack trace.
As such, I cannot really see a big disadvantage. The only difference is, that you have 2 anonymous calls instead of one. OTOH, in performance critical situations, you shouldn't be using either (except one internal timer) to avoid frequent instantiation of TimerEvent
objects.
Basically, I think it's a matter of taste. Adobe decided, the AS3 event system is the shizzle, so they promote it.
Timer:
Gives you more control as you can register more event listeners to receive the event rather than a single one with setTimeout
You can control the start time and
the number of repetitions ( not very
useful against setTimeout, as this
has to run just once and after a
delay considering the immediate time
it was called)
setTimeout:
Easier to use
Less code to write
Parameters can be easily sent;
I prefer the Timer class but I've seen setTimeout being frequently used by programmers.
Also if you are using Tweening libraries,some support delayed call
For example TweenMax TweenMax.delayedCall(2, myFunction, ["myParam"]);
For all those who say that setTimeout is deprecated, this is non sense..
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#setTimeout%28%29
I believe you can't see any "deprecated" keyword around setTimeout here
setTimeout is working perfectly well in external .as files.
Just use this in the class :
import flash.utils.*;
import flash.events.TimerEvent;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With