Skip to main content
Known Participant
March 28, 2010
Question

handle timeout event

  • March 28, 2010
  • 4 replies
  • 896 views

Hi I'm running a recursive function which in the worst case senario is of factorial order. (i.e: runs itself n! times)

As soon as n exceeds 7 in these worst case senario's the function times out (Error #1502). I want to handle this error event so that the user can be presented with an error that the problem is too complex.

I don't want to extend the timeout period as 15 seconds is reasonable.

And please don't suggest restricting n as the function is only of factorial order in the worst case senario

How can this error be captured/handeld

thanks in advance

This topic has been closed for replies.

4 replies

Inspiring
March 29, 2010

I found what you're looking for... You can use the ScriptTimeoutError under flash.errors package: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/errors/ScriptTimeoutError.html

Inspiring
March 29, 2010

How about use a timer and a flag that stops recursion after a timeout?

var fCalculate:Boolean = true;

var cTimer:Timer = new Timer(12000, 1); //Timeout less than 15 sec (12.0 sec)

cTimer.addEventListener(TimerEvent.TIMER, onTimerExpired);

cTimer.start(); //Start timer just before your code starts

var nResultant:Number = YourExecutionCode();

cTimer.stop(); //Stop after execution

function YourExecutionCode():Number

{

    //Also try to place those conditions in loops to break them with break;

    if(fCalculate == true) YourExecutionCode();

    else trace("ERROR: Operation took too long");

}

function onTimerExpired(oEvent:TimerEvent):void

{

    fCalculate = false;

}

Inspiring
March 29, 2010

I'm curious.. why would your code for factorial operation take that long? Even a calculator can solve a maximum of 69 factorial in a fraction of a second. "n" can be made as large as 34 using uint type and as large as 6149 using Number type before it would reach a stack overflow error in a fraction of a second.

var nResult:Number = factorial(6149);

trace(nResult);

function factorial(n:uint):Number

{

    if(n > 1) return n*factorial(n-1);

    else return 1;

}

Participating Frequently
March 29, 2010

He's not making a factorial function - he's making an algorithm which has a worst case scenario of O(n!) - http://en.wikipedia.org/wiki/Big_O_Notation

Inspiring
March 29, 2010

Oh okay. Thank you corrundom for feeding my curiosity hmmm.. Travelling Salesman Problem...

Participating Frequently
March 29, 2010

Hmm, well does a regular:

try {...} catch (err:Error) {}

work? I'm guessing not - Flash seems to automatically catch this type of error.

Maybe you can have a counter that automatically ends the recursion after so many times?