Skip to content

Delaying a function call

Categories: Flash

Table of Contents

What I’ve always found a major pain in Flash was delaying a function call from being called for a short period of time. The only reasonable way to do it was to call setInterval() and then clearInterval() after the function was called. I had to keep a intermediate variable to keep the interval id that setInterval() returned. This was a major pain and often ended up in cluttering up my project with code like this:

function delay() {
callOtherFunction();
clearInterval(intervalId);
}

I hate having to make intermediate variable & functions to accomplish really simple task. I came across Tom Rethaller’s schedule() method on proto.layer51.com and it was just what i was looking for except it had some scope issues and wouldn’t allow you to schedule the same function to be called on multiple instances of a object. I fixed up the code and posted is on proto.layer51 as well as on this site. With this prototype addition, you can simply call the schedule() method on a function to delay it like this:

aFunction.schedule(1000, scopeObject);

and ‘aFunction’ will be called in 1 second in the scope of ‘scopeObject’.
Hopefully you’ll find this method as useful as I do! 🙂