5

I want my code to wait for a couple of seconds before executing. So is there any function similar to sleep in flex?

1

4 Answers 4

16

There is no sleep or delay function in ActionScript. Like JavaScript, you can use setTimeout() instead like this:

function trigger():void { setTimeout(doIt, 1000); }
function doIt():void    { Alert.show("done!"); }

Once you link the trigger() function to any event such as ‘click’, when the event happens alert box will show up after 1 sec.

There is also setInterval() and clearlnterval() functions you can use for repetition. However, it is recommended to use the flash.utils.Timer class in that case.

private var myTimer:Timer;

private function init():void {
    myTimer = new Timer(5000, 1);
    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
    myTimer.start();
}

public function timerHandler(event:TimerEvent):void {
    Alert.show("I was delayed!");
}
2
  • 2
    I would recommend that you make an example using Timer and not setTimeout.
    – J_A_X
    Mar 21, 2012 at 19:31
  • 1
    FYI if you must use setTimeOut, be sure to call clearTimeOut, or you risk a memory leak. However, I wouldn't be so quick to just answer the question here...I suspect there's an underlying misunderstanding of the asynchronous nature of flash/flex that we'd otherwise have an opportunity to clear up. Mar 21, 2012 at 22:23
2

It always depends on what you are trying to do.

I see you are using Flex. There a neat feature in Flex that's called callLater, wich is impletemented in UIComponent. It's similar to setTimeout in use but the function will be automatically called on the next update cycle instead of a set interval of time. Suppose you set some data to a datagrid and what to select a specific cell/row in it. You'll use callLater to ensure the datagrid had time to process the data. Here's an exemple :

protected function dummy():void
{
    myComponent.callLater(myFunction, ["this is a message"])
}

protected function myFunction(message:String):void
{
    Alert.show(message);
}

If you just want to delay an execution one time, setTimeout is the way to go. If you want to execute something more than once at defined intervals, use Timer indeed.

If it's to react to some action, like a remote save function, I suggest you use events instead and listen for something like a SAVE_COMPLETE event.

1
  • +1 for at least mentioning that Events are likely the way to solve whateveer the underlying problem is the OP is trying to solve! Mar 21, 2012 at 22:24
2

There is no sleep function in ActionScript. Everything runs in one thread, so it would also block all user interface interactions, which is commonly perceived as a bad way to go.

The closest option is to use the Timer class which would only be started in the function in question and "fire" the code you wanted to wait after it completes it's 2 second wait.

in function:

private function whereWeStartTimer():void{
    //misc code that you always execute before starting your timer

    var timer:Timer = new Timer(2000); // 2 second wait
    timer.addEventListener(TimerEvent.TIMER,functionTimerFlagged);
    timer.start();
}

private function functionTimerFlagged(event:TimerEvent):void{
    var targetTimer:Timer = event.target as Timer;
    targetTimer.removeEventListener(TimerEvent.TIMER,functionTimerFlagged);
    targetTimer.stop();

    //put your code here that you wanted to execute after two seconds

    //force-ably destroy timer to ensure garbage collection(optional)
    targetTimer = null;
}
0

What if I had to execute the timer button like in the following condition? I have one map click event which shows info on map window. I need to fetch information from DB in between. Now to show the fetched info in the popup of info, it takes time.

I think its better to induce a sleep or timer in between map click and info popup, so that fetched data can be shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.