Does actionscript 2.0/3.0 have an equivalent of c# 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.
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.
sleep(100, trace, ['Hello']);
The advantage of this approach is that the UI is still responsive during the sleep.