Timeouts in ActionScript

Timeouts are important for programs that connect to external resources or that otherwise need to bound execution time. Implementing timeouts in ActionScript requires careful management of asynchronous operations and timers.

package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class Timeouts extends Sprite {
        public function Timeouts() {
            // For our example, suppose we're executing an external
            // call that returns its result after 2 seconds.
            executeWithTimeout(2000, function(result:String):void {
                trace(result);
            }, function():void {
                trace("timeout 1");
            });

            // If we allow a longer timeout of 3s, then the operation
            // will succeed and we'll print the result.
            executeWithTimeout(3000, function(result:String):void {
                trace(result);
            }, function():void {
                trace("timeout 2");
            });
        }

        private function executeWithTimeout(timeout:Number, 
                                            onSuccess:Function, 
                                            onTimeout:Function):void {
            var timer:Timer = new Timer(timeout, 1);
            var operationComplete:Boolean = false;

            timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
                if (!operationComplete) {
                    onTimeout();
                }
            });

            timer.start();

            // Simulate an asynchronous operation
            var operationTimer:Timer = new Timer(2000, 1);
            operationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
                if (!timer.running) return; // Operation completed after timeout

                operationComplete = true;
                timer.stop();
                onSuccess("result");
            });

            operationTimer.start();
        }
    }
}

In this ActionScript example, we’re simulating timeouts using the Timer class. The executeWithTimeout function takes a timeout duration, a success callback, and a timeout callback.

For each operation:

  1. We create a timer with the specified timeout duration.
  2. We start an asynchronous operation (simulated with another timer).
  3. If the operation completes before the timeout, we call the success callback.
  4. If the timeout occurs before the operation completes, we call the timeout callback.

Running this program shows the first operation timing out and the second succeeding:

timeout 1
result

This approach demonstrates how to implement timeout behavior in ActionScript, although it’s worth noting that ActionScript doesn’t have built-in constructs like Go’s select statement for handling multiple asynchronous operations concurrently. In more complex scenarios, you might need to use additional patterns or libraries for managing asynchronous flows.