How to halt execution in ActionScript
Asked Answered
S

4

6

Is there any way to halt execution in ActionScript, such as a sleep() method?

I know that there is a setTimeout() method, but setTimeout() just sets up an event for deferred execution.

Salutatory answered 1/10, 2009 at 22:32 Comment(2)
I just have to say neato on the tag icons! ;-)Lyssa
Why would you want to sleep() in a language that has only one controllable thread of execution?Unalterable
S
3

No. There is no sleep. Sorry.

See my answer here for options: ActionScript: pushing a closure onto the event stack?. It doesn't talk about sleeping, but I tried to provide an overview of deferred function calling.

Shaynashayne answered 1/10, 2009 at 23:4 Comment(0)
U
1

You need to think in terms of not sleeping. Actionscript is not that kind of language. Because the flash player alternates between frame renders and code execution, sleeping in the code is always a bad idea, which is why there is no method to do it.

Having said that, you could achieve this by using the blocking method ExternalInterface.call, and in Javascript executing a blocking method (like XHR request).

Absolutely idiotic though, so don't do it.

Perhaps what you need is a Timer.

Unscathed answered 10/6, 2011 at 14:13 Comment(0)
C
0

There's no way to pause all execution of an application as in PHP, but there are workarounds (unless you set a breakpoint or create a runtime error on purpose, don't think that's what you meant). Probably this is because usually flash applications are meant to execute all the scripts in less than one "frame".

It's common to be able to "pause" the animations of a website when the user unfocus it. This can be made by listening to Event.DEACTIVATE and then remove the ENTER_FRAME listeners and kill all ongoing processes.

You could also create a central EventDispatcher to replace the internal ENTER_FRAME, this way you seamlessly control speed of execution as well as pausing / resuming (won't stop executing scripts, nor asynchronous handlers such as loaders etc. though).

Curling answered 1/10, 2009 at 23:7 Comment(1)
There's a hack :) See my answer.Perissodactyl
P
-4

Yes, there is, though be aware of the 15 second script timeout. ( You can change that 15 second script timeout in the Publish Settings... )

I've found in the past that if you're looking for this functionality, you're doing something wrong :)

Whatever you're trying to accomplish is probably calling for an Event listener instead.

//adding this ENTER_FRAME event listener just to show that the script pauses for one
// second before the first frame executes
addEventListener( Event.ENTER_FRAME, onFrame );

function onFrame( event:Event ):void {

    trace( "first frame occurs after pause of", getTimer() + " ms" );
    removeEventListener( Event.ENTER_FRAME, onFrame );

};

var startTime:int = getTimer();
var pauseTime:int = 1000;

while( ( getTimer() - startTime ) < pauseTime ) {
    //do nothing... we're effectively pausing here...
}
Perissodactyl answered 2/10, 2009 at 2:1 Comment(2)
This is not a sleep. You'll hit 100% CPU in the while loop. This is actually much worse than having a setTimeout call set to 1000. The only difference is that here, because of the CPU usage, you are effectively locking the program from executing any other "threads".Shaynashayne
That's a spin wait, not a sleep, and unless you seriously know what you are doing, very bad form.Unscathed

© 2022 - 2024 — McMap. All rights reserved.