Skip to content


setTimeout function. Nice little treat

Have you ever been working on a project and get to a spot where you need to call a function but need it to wait a few seconds before it calls it? Oh yeah, you could always use a tween engine like GreenSock to do it, like

TweenLite.to(this, secondsToWait, {onComplete:functionCalled});

but that is a little clunky and not the best way to do things.

So to answer my question above there is a function in AS that will do this for you and it is called setTimeout(). I’m sure a lot of you know about this, but a lot of people don’t.

This is a function for a one time timer, or a one time delay if you will. The way you use it is first import it.

import flash.utils.setTimeout;

then you will want to call it when you need it like this:

setTimeout(functionToCall, delayInMilliseconds or 2000, parameter, parameter);

You do not need to pass any parameters to it but if the function you are calling needs them that is where you would place them.

This little snippet will save you a little time and headaches later.

Share and Enjoy:
  • Facebook
  • Twitter
  • del.icio.us
  • Google Bookmarks

Posted in ActioinScript 3.

Tagged with , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Tahir Ahmed says

    TweenLite also has a method for “delayed calls” … something like this:

    TweenLite.delayedCall ( delay, functionToCall, [arrayOfParams] ); where [arrayOfParams] is optional.

    I use that quite often instead of setTimeOut.

    Regards.

    • Todd says

      That is nice to know. I have never used that function in TweenLite so thank you for posting. The only thing I would have to say about using TweenLite is if you are trying to keep your file size very small.



Some HTML is OK

or, reply to this post via trackback.