Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for 3 seconds in ActionScript 2 or 3?

Tags:

actionscript

Is there any way to implement waiting for, say, 3 seconds in ActionScript, but to stay within same function? I have looked setInterval, setTimeOut and similar functions, but what I really need is this:

public function foo(param1, param2, param3) {
  //do something here
  //wait for 3 seconds
  //3 seconds have passed, now do something more
}

In case you wonder why I need this - it is a legal requirement, and no, I can't change it.

like image 342
TheJavaGuy-Ivan Milosavljević Avatar asked Nov 08 '11 14:11

TheJavaGuy-Ivan Milosavljević


People also ask

What is the difference using ActionScript 2.0 and ActionScript 3.0 for controlling video?

The main difference is that you can develop flash applications with a much stronger OOP influence than in AS2. AS3 makes it much easier to utilise third party code such as Greensock's Tweenlite, Papervision 3D and box2d.

What does ActionScript 3 do?

Action Script 3.0 in Flash allows you to create all kinds of fully interactive applications such as dynamic websites and computer games. Also, AS3. 0 is an object-oriented programming language; if you are familiar with AS3. 0, it will help you to learn other object-oriented language such as Javascript.

What is a method in ActionScript?

A function is called a method if you define it as part of a class definition or attach it to an instance of an object. A function is called a function closure if it is defined in any other way. Functions have always been extremely important in ActionScript.

Is ActionScript still used?

The use of ActionScript 3 is dwindling. Adobe has announced their plans to end support for the technology in 2020, and many entities that are crucial to the Flash ecosystem may drop support earlier than that.


2 Answers

Use the Timer to call a function after 3 seconds.

var timer:Timer = new Timer(3000);
timer.addEventListener(TimerEvent.TIMER, callback); // will call callback()
timer.start();

To do this properly, you should create the timer as an instance variable so you can remove the listener and the timer instance when the function is called, to avoid leaks.

class Test {
    private var timer:Timer = new Timer(3000);

    public function foo(param1:int, param2:int, param3:int):void {
        // do something here
        timer.addEventListener(TimerEvent.TIMER, fooPartTwo);
        timer.start();
    }

    private function fooPartTwo(event:TimerEvent):void {
        timer.removeEventListener(TimerEvent.TIMER, fooPartTwo);
        timer = null;
        // 3 seconds have passed, now do something more
    }
}

You could also use another function inside your foo function and retain scope, so you don't need to pass variables around.

function foo(param1:int, param2:int, param3:int):void {
    var x:int = 2; // you can use variables as you would normally

    // do something here

    var timer:Timer = new Timer(3000);
    var afterWaiting:Function = function(event:TimerEvent):void {
       timer.removeEventListener(TimerEvent.TIMER, afterWaiting);
       timer = null;

       // 3 seconds have passed, now do something more

       // the scope is retained and you can still refer to the variables you
       // used earlier
       x += 2;
    }

    timer.addEventListener(TimerEvent.TIMER, afterWaiting);
    timer.start();
}
like image 95
rid Avatar answered Oct 24 '22 21:10

rid


For AS3 use Radu's answer.

For AS2 use the setInterval function like so:

var timer = setInterval(function, 3000, param1, param2);

function (param1, param2) {

// your function here
clearInterval(timer);

}
like image 21
George Reith Avatar answered Oct 24 '22 23:10

George Reith