view source

Wait just a second

November 2nd, 2006 . by polyGeek

How many times have you been coding Actionscript and needed to run some code in a second, or two, or whatever? I know, you can create a setInterval, blah, blah, blah. But that’s such a pain in the the ass.

So, I made a class with just one method to do make it easier for you to wait a second. If you need to call a function/method in, say half-a-second, then all you would write is this:

Wait.For(500, myMethod).

Done.

But, wait, there’s more. You can pass arguments that you need to send to your function/method. Just like this:

Wait.For(500, myMethod, “coffee”, “hot”, “cream”, suger”);

is that sweet or what?

There is a limit of 10 arguments that you can pass. If you really need to send more then edit the class yourself.

Here’s the code for the class below or you can download the file: Wait.as - Wait.zip.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

10 Responses to “Wait just a second”

  1. comment number 1 by: Sergi Mansilla

    There is an undocumented function in Flash that does that: setTimeout. It works very similar to setInterval.
    You can find out how to use it at http://www.flashguru.co.uk/flash-8-settimeout/

    By the way, you should use dynamic arguments. The way you send arguments to the callback function is not very elegant, and it limits the amount of arguments to send. Take a look to this:
    http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary369.html

    Cheers

  2. comment number 2 by: Rasmus

    Or you could use:
    callback(a.slice(2, a.length));

    to pass as many arguments as needed

  3. comment number 3 by: polyGeek

    @Rasmus, true enough. Good thanking.

  4. comment number 4 by: Andy

    Flash already has this:

    setTimeout(scope, “function”, milliseconds, param1, param2, etc);

  5. comment number 5 by: polyGeek

    @Andy, you sure that isn’t a custom class you wrote? setTimeout isn’t in the help files and the following code doesn’t do anything:

    setTimeout(this, CB, 100, “hi”);

    function CB(arg:String):Void {
    trace(arg);
    }

  6. comment number 6 by: Rasmus

    setTimeout is an undocumented function introduced in Flash 8. For more info see this post from Flashguru: http://www.flashguru.co.uk/flash-8-settimeout/

  7. comment number 7 by: polyGeek

    @Rasmus, wonderful, thanks for pointing this out. The problem with the code above is that here is no ’scope’ parameter. So this works as expected.

    setTimeout(CB, 1000, “hi”);

    function CB(s:String):Void {
    trace(s);
    }

  8. comment number 8 by: wolfito

    did you try _global.setTimeout(CB, 1000, “hi”); ?

    i use that when i need a method call to wait

    :)

  9. comment number 9 by: wolfito

    sorry i forgot the scope:

    (inside a class)

    timerInterval = _global.setTimeout(this, “functionName”, 100);

  10. comment number 10 by: polyGeek

    @wolfito, you know, I’ve never really used _global before. I think static classes do the same thing for me.

    By the way, wicked cool sight you have. I can’t read it but I love the detail you put into it. Something as simple as making the text coast to a stop when scrolling is a very sweet effect and looks quite professional. Kudos.

Leave a Reply

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites