setTimeout() “Gotcha” in Class Files
The setTimeout() function is a valid citizen in the realm of ActionScript 2.0; it was simply left off the roster for some reason, so you won’t find it in the documentation. It works very much like its JavaScript counterpart and is less cumbersome to use than setInterval() for triggering a single delayed action. You can reference setTimeout() just fine in timeline code, but I found an unexpected problem when employing this function in a class file. Its presence halted the compile process and caused all sorts of misleading errors, such as the idea that Stage can’t be reference in a class file (it certainly can). Is there a workaround? Yes.
An answer, short and sweet
If you find yourself desiring setTimeout() inside a custom class, use the array access operator to refer to the function.
_global["setTimeout"]
This one of the techniques you can use to reference objects dynamically — and even functions are objects. To actually trigger the function, you need to add the parentheses, and those come right after the closing bracket. At that point, it’s business as usual.
_global["setTimeout"](functionToTrigger, 1000);
Note: See Update comment below.
How it works
To be honest, I can’t explain the “how” especially well, in this case. The setTimeout() function was left out of the documentation and the intrinsic class definition files, which means the compiler (sometimes) doesn’t know it exists. Why this only happens in external class files is beyond my grasp. But the solution is easy, enough.
I had to experiment with which object to use as the prefix (I tried _root, a passed-in reference to the main timeline, and this, but suddenly _global occurred to me, and that’s what works).
Update! Actually, it gets even easier. All you really need is the _global reference. The array access operator isn’t necessary.
_global.setTimeout(functionToTrigger, 1000);
Thanks to Davel_x for pointing this out!
January 17th, 2007 at 12:28 pm
According to this page :
http://wiki.media-box.net/tutoriaux/flash/settimeout
simply typing _global.setTimeout() works too, have you tried ?
I didn’t know this function and I needed it just… now !
I could have done things with a setInterval but this is much more clean
Thanks !
January 17th, 2007 at 1:55 pm
Davel_x,
Ha! Yes, you’re right. The array access operator isn’t needed. Thanks for pointing that out!
January 20th, 2007 at 1:23 pm
Also, when you use eval(”this.somestring”) or this[”somestring”], it is compiled as this.somestring because the compiler knows that when evaluating a literal string, it will have the same end result as using that literal string as a variable. So if _global[”setTimeout”] does work, then surely _global.setTimeout would too.
January 20th, 2007 at 6:45 pm
NSurveyor,
That makes sense. The array access operator does, however, allow references to illegal variable names such as numerals. That prompts all sorts of new questions for me on how the compiler works.
I just tested
_global. n = 5;, for example. That two-character propertynshouldn’t work, because the first character is a space — yet it does.January 20th, 2007 at 10:14 pm
Good point. I’ve been putting lines of code such as: _global[” n”];, compiling, and then decompiling, to see how it works. And after a little bit of testing, I find that if it is an illegal variable, it will not remove the eval/array access operator, otherwise it will.
August 16th, 2007 at 8:44 pm
I know this is from a while ago, but yet again I was directed to your site for help
It took me a little while to find out how to call a function in setTimout if the function has arguments, so I thought I’d share it here.
You have to call them after the interval:
setTimeout( obj, methodName:String , duration:Number, [arg1, arg2, …]) ;
Source: http://wiki.mediabox.fr/tutoriaux/flash/settimeout
August 17th, 2007 at 9:35 am
annie,
Thanks for that.
You’re right, it isn’t especially obvious how to use arguments with
setInterval(). In addition, this function happens to have two alternate uses, as you’ve shown in your reply: an object reference may optionally be supplied as the first parameter, shoving the remaining parameters over a slot. The object reference affords basically the same benefit as using theDelegate.create()method.