Action Script Sleep function

31,681

Solution 1

Not really. You could block (almost all) code execution with something like:

function sleep(ms:int):void {
    var init:int = getTimer();
    while(true) {
        if(getTimer() - init >= ms) {
            break;
        }
    }
}

trace("hello");
trace(getTimer());
sleep(5000);
trace("bye");
trace(getTimer());

But I don't see how could this be useful in flash. And, at the same time, anything like the above code is a very bad idea as the player will freeze and become unresponsive (and could also give a script timeout if you exceed the timeout limit, which is 15 by default).

If you merely want to delay the execution of a piece of code, you can use a Timer object or the setTimeout function. This will be non-blocking, though, so you'll have to use some kind of flag like TandemAdam suggested. It'll be brittle, at best.

Maybe there's a better approach for your problem, but it's not clear what are you trying to accomplish in your question.

Solution 2

You can implement a sleep function like this:

function sleep(counter: int, subsequentFunction: Function, args: Array): void
{
    if (counter > 0)
        callLater(sleep, [counter - 1, subsequentFunction, args]);
    else
        callLater(subsequentFunction, args);
}

Call it with the function which should be processed after the pause.

// call trace('Hello') after 100 cycles
sleep(100, trace, ['Hello']);
// call myFunction() after 50 cycles
sleep(50, myFunction, []);

The advantage of this approach is that the UI is still responsive during the sleep.

Solution 3

No ActionScript/Flash Player does not have an equivalent to the c# sleep function. For one thing Flash does not use multiple Threads.

You would have to implement the functionality manually.

You could use a Boolean flag, that your code would only execute when true. Then use the Timer class, for the delay.

Share:
31,681
Kemrop
Author by

Kemrop

Updated on July 05, 2022

Comments

  • Kemrop
    Kemrop almost 2 years

    Does actionscript 2.0/3.0 have an equivalent of c# sleep() ?

    • Bitterblue
      Bitterblue about 10 years
      Who marked this question as duplicate since the real and correct answer is only here in this thread below ? It's a good question and a good answer.
  • Kemrop
    Kemrop almost 14 years
    Thanks i could use this..Just wanted to delay the execution of some block of code by less than 100ms.
  • IneedHelp
    IneedHelp over 12 years
    Hello! How would the callLater function look like?
  • splash
    splash over 12 years
    @IneedHelp: see my updated answer
  • Worker
    Worker over 11 years
    That is a good code for testing purposes! +1
  • sleepycal
    sleepycal almost 10 years
    This is a completely unnecessary use of CPU, and almost certainly should be downvoted.
  • Max A.
    Max A. over 9 years
    This is great for testing purposes. It's very useful for debugging timing issues. Obviously, it should not be used in production.
  • Tox
    Tox almost 4 years
    works like a charm, thanks a lot!