Timer vs setTimeout

18,194

Solution 1

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.

Solution 2

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)

  • More lines to write, even more if you need to differentiate with parameters ( custom event class for this )
  • Use of event listeners which is
    standard practice in as3.
  • Cleaner look

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

Solution 3

setTimeout is working perfectly well in external .as files.

Just use this in the class :

import flash.utils.*;
import flash.events.TimerEvent;
Share:
18,194
Christophe Herreman
Author by

Christophe Herreman

Interested in: Object technologies, Domain-Driven Design, Architecture, Java, Flex, User eXperience, usability... Spring ActionScript project lead and open source enthousiast.

Updated on July 17, 2022

Comments

  • Christophe Herreman
    Christophe Herreman almost 2 years

    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.

  • back2dos
    back2dos about 14 years
    hi . nice to see you here :) anyway: could you go a little more into detail? what do you mean by "external"?
  • back2dos
    back2dos about 14 years
    generally a good reasoning, but unlikely to happen with flash player. adobe really cares a lot about backward compatibility and legacy support. also, in this case, one can rewrite the function within a couple of lines.
  • Martin Konecny
    Martin Konecny about 14 years
    @back2dos, yes you are right in the case with Adobe, but why not follow good practices in general?
  • Cristi Băluță
    Cristi Băluță about 14 years
    By external .as files i mean classes. Is anyone here using as3 without classes? PS: Do we know each other?
  • back2dos
    back2dos about 14 years
    Strange. Works perfectly for me, no matter whether compiled with the IDE or mxmlc. PS: not personally, but from the haXe mailing list. :)
  • Cristi Băluță
    Cristi Băluță about 14 years
    Oh, sorry for the misinformation then, seems i can't reproduce it myself. Probably the compiler was confused by another error and gave me this one. Was for a flash project after 2 years of programming only in haxe :P
  • back2dos
    back2dos about 14 years
    what other than Adobe's opinion makes timers a better practice than setTimeout? I agree, a Timer offers more possibilities then setInterval and setTimer, but then again, AVM2 bytecode offers more possibilities than ActionScript :D I think setTimeout is a useful function. If it weren't there, I'd write it on my own. To me, it is good practice for code to be concise, because that makes it readable and understandable. so while I generally agree, deprecation is usually to be taken seriously, in the case of ActionScript 3, I often feel it's really just a matter of taste.
  • Oliver
    Oliver about 14 years
    I was just going to correct you after a talk with Virusescu :)
  • Pier
    Pier over 11 years
    setTimeout works perfectly in any .as file. Just import flash.utils.setTimeout