Is there a sleep function in flex?

I want my code to wait a couple of seconds before it executes. So, is there any kind of sleep-like function in flex?

+5
source share
4 answers

ActionScript does not have a sleep or delay function. Like JavaScript, you can use setTimeout()as follows:

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

As soon as you associate the function trigger()with any event, such as “click”, when the event appears, a notification window will appear after 1 second.

There are also functions setInterval()and clearlnterval()that you can use to repeat. However, in this case, it is recommended to use a class flash.utils.Timer.

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!");
}
+15
source

It always depends on what you are trying to do.

, Flex. Flex , callLater, UIComponent. setTimeout, . , datagrid / . callLater, , datagrid . :

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

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

, setTimeout - . - , .

- , , - SAVE_COMPLETE.

+2

ActionScript . , , .

- Timer, "" , , . 2 .

:

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;
}
+2
source

What should I do if I needed to start the timer button, as in the following condition? I have one map click event that displays information about the map window. I need to get information from the DB between them. Now, it takes time to display the extracted information in a pop-up information window.

I think it’s best to trigger a sleep or timer between the click of the mouse and the popup so that the displayed data can be displayed.

0
source

All Articles