开发者

Action Script Sleep function [duplicate]

This question already开发者_Python百科 has answers here: actionscript 3 sleep? (2 answers) Closed 9 years ago.

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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜