setTimeout():  An “Undocumented” Documented Function

ActionScript 2.0

In an earlier article, we looked at How to Pause a Timeline, which made use of the global setInterval() function to get the timeline rolling again after a specified length of time.  setInterval() has been available since Flash MX (aka Flash 6) and from its inception, has often caused developers to furrow their brows and wonder:  if this function is just like the setInterval() function in JavaScript — and it basically is — why doesn’t ActionScript also feature a setTimeout() function, like JavaScript does? 

Alas, I cannot answer that question.  ;)   But I can point out that ActionScript does support this function now, since Flash 8.  It is a bona fide, documented feature — only, it was inadvertently left out of the documentation, according to the LiveDocs comments here (scroll to the bottom):

http:// livedocs.macromedia.com/flash/8/main/00001717.html

A quick look at setInterval()

setInterval() triggers a user-specified function repeatedly, so to make it trigger something only once, you must capture its return value and use that value as the parameter of its companion function, clearInterval(), inside the triggered function.  Sounds like a mouthful, right?  Here’s a quick example.  The following ActionScript sends a message to the Output panel after two seconds.  It repeats this message every two seconds.

setInterval(function(){trace("message");}, 2000);

Now, if that looks like a bunch of gobbledygook, let’s “rephrase it.”

setInterval(function() {
  trace("message");
}, 2000);

The line breaks really don’t matter, as long as the code is correct.  setInterval(), at its most basic use, accepts two parameters:  a function to trigger and a time at which to do so.

setInterval(function here, time here);

The time is supplied in milliseconds, which is why two seconds looks like two thousand.

setInterval(function here, 2000);

And the function may either be a named function …

function sample() {
  trace("message");
}
setInterval(sample, 2000);

… or a function literal …

setInterval(function() {
  trace("message");
}, 2000);

So far, so good, right?  But as we’ve already seen, this repeats the specified function continuously.  If we only want a single delayed trigger, we need to use the global clearInterval() function to “kill” the cycle.  Thing is, it’s entirely possible to have a number of cycles running at once.  How would clearInterval() know which interval to clear?

As it turns out, setInterval() returns a value, listed in the ActionScript Language Reference as its “interval ID.”  There’s nothing “special” about this value:  it’s just an integer.  You could invoke clearInterval() and simply put a number inside the parentheses, but again, which number would you supply?

The easiest thing is to store setInterval()’s return value in a variable and use that variable in the parentheses.

var id:Number = setInterval(function(){
  trace("message");
  clearInterval(id);
}, 2000);

Nice and neat.  Since setInterval() was introduced, this has been the way to wrangle it into use as a delayed–function trigger.

A quick look at setTimeout()

The new setTimeout() function makes things even easier.  Its specific purpose is to trigger a function once, at the specified delay.  Use it if you like, but be advised, its odd status as an “undocumented” documented function means we don’t have a proper ActionScript Language Reference entry for guidance.  The following sample works for me in SWFs published as far back as Flash Player 5, regardless of settings for ActionScript 1.0 or 2.0. Of course, that may only be true because I have the Flash Player 8 plug-in installed.

setTimeout(function(){trace("message");}, 2000);

Note, too, there exists a companion clearTimeout() function.  So what’s it for, if setTimeout() only triggers things once?  Well, imagine you have something set to go off — say, a warning — in ten seconds’ time.  You  only need to warn the user under certain circumstances that happen to be true at the moment.  If the user changes these circumstances, you no longer need to trigger the warning.  How do you stop it?

var id:Number = setTimeout(function() {
  trace("message");
}, 2000);
clearTimeout(id);

Leave a Reply