Joy with the script timeout period

Isn’t it just great when you finally find a useful new feature in Flash 9.
I’m talking about the script-timeout period.
Remember how you sometimes had to run a script for more than 15 seconds and always got that annoying timeout popup? From now on (using Flash 9 / player 7 and up) you can set the timeout as you wish. Isn’t that just great?!
It gets even better when you use AS3; the player doesn’t just show the script-timeout popup but it actually throws an Error (“Error #1502: A script has executed for longer than the default timeout period of 15 seconds.”). This way you can make a certain part of your script (which needs more time than the supplied timeout period) run over and over again until the calculation is complete.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package { 
	import flash.events.Event; 
	import flash.display.MovieClip; 
	public class TestMC extends MovieClip{ 
		private var i:int = 0; 
		private var loopCount:int = 0; 
		public function TestMC() { 
			this.addEventListener(Event.ENTER_FRAME, this.calculate); 
		} 
		private function calculate(event:Event):void { 
			try{ 
				while(true){ this.i++; } 
			} 
			catch(e:Error){ 
				this.loopCount++; 
				trace("____________n'i' after "+loopCount+" frames = " + this.i); 
				if(i<0){ //i will be negative when it becomes to big, normally you would check here if your calculation is done... 
					this.removeEventListener(Event.ENTER_FRAME, this.calculate); 
				} 
			} 
		} 
	} 
}

This is just an example and I’m sure you can think of much more useful ways to use this feature.
Hope it heps you along…

Update (1 December 2007):
Just great; I forgot to mention HOW to set the timeout . Well here is how:
You can’t set the timeoutperiod by code (neither in as3 nor in as2). You have to set the timeout manually in the publish settings of your fla: Ctrl+Shift+F12 > flash-tab > “Script time limit”.
Let me know if you still have trouble.

16 Responses to “Joy with the script timeout period”


  • Finally, this is just great when parsing (an xml or something). Sometimes parsing takes a long time which now can be catched before an (in this case) annoying popup appears!

    Great….

  • Just happened by your site by one of the FlexBlogs and couldn’t not just toss out a ‘Hey’.

    hey
    –steve…

    …as this this is of social, non-flex nature feel free to not post this.

  • Hi, I would love to know how to set the script timeout limit in actionscript 2.0. Your example didn’t display for me in your blog, but I viewed source and tracked it down on your server, then decompiled it – however, I couldn’t find any reference to setting the script timeout in the decompiled code…

    Could you please post the syntax to set the script timeout for actionscript 2.0?

    please please please

    Thanks,

    Gabriel

  • There was some trouble with the FlashTextFormatter plugin so you couldn’t read the code. But I got rid of the plugin so you should be able to understand the example now.
    Infact you can’t set the timeoutperiod by code (neither in as3 nor in as2). You have to set the timeout manually in the publish settings of your fla:
    Ctrl+Shift+F12 > flash-tab > “Script time limit”.

  • Thank you for this! Is there a way of calling a function once the error is identified? I have been trying to split this ‘issue’ into two parts:
    1, Display UI feedback to visitor instead of time_out
    2, Proceed with load.

    In my current project, the primary dataset exceeds 2Mb. It times_out with reckless abandon, so it’d be nice to inform visitors that something is happening… ;)

  • Hi Chapp,

    Using a Timer instead of an EnterFrame there is a little more time between the calculations so the you can run some other code to inform your visitors.
    In this example I used a trace but in contrast to the EnterFrame example this code will show the traces in between the timeouts (instead of saving them and only showing them at the end when the complete calculation is done).

    Here is the new code:

    package {
    import flash.events.TimerEvent;
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.utils.getTimer;
    public class TestMC extends MovieClip{
    private var i:int = 0;
    private var timer:Timer;
    public function TestMC() {
    this.timer = new Timer(100, 0);
    this.timer.addEventListener(TimerEvent.TIMER, this.calculate);
    this.timer.start();
    }
    private function calculate(event:TimerEvent):void {
    try{
    while(true){ this.i++; }
    }
    catch(e:Error){
    trace(“____________\nTimeout #”+this.timer.currentCount+”: ‘i’ after “+getTimer()+” ms = ” + this.i);
    if(i<0){ //i will be negative when it becomes to big, normally you would check here if your calculation is done…
    this.timer.removeEventListener(TimerEvent.TIMER, this.calculate);
    }
    }
    }
    }
    }

  • ah, smart thinking! thank you for the tip – i’ll try it out this afternoon. could this be used mid-render, perhaps to indicate ‘render done %’? once the data has loaded, it would make sense to provide a secondary loader to indicate why there’s a secondary wait, perhaps as an incremental note?

  • Hey, just wanna say thanks, I scoured the internet trying to figure out why my xml parsing loop was timing out at around a couple thousand nodes, I eventually figured out it was a silent timeout but could not find a solution for the life of me how to deal with it, setting the script timeout limit did nothing for me, but this helped me tremendously! Thanks again.

  • Hi, chapp

    if I’m initializing an actionscript3 project in flexbuilder, Couldn’t I use ‘Ctrl + shift + F12′ to set script time limit?
    seems your way to set time limit is based on flash cs3, etc.

  • I’m not really into Flex so I have no idea on how to set the timeout in Flex.

  • mxmlc takes the option “-default-script-limits ”
    found over here:
    http://www.actionscript.org/forums/showthread.php3?t=185320
    just i case You’re rollin’ with the command line compiler…

  • @dbam
    Thanks dbam, i think i found my answer from your reply.

  • I have an employee shift scheduling application using flash 4, actionscript 2,amfphp, mysql and php. My problem is this. When the application is preparing a master schedule a large matrix is used. I get at least 4-5 time out warnings (every 15 seconds). I am preparing to roll out this application and I need a solution since the end user will not accept this delay. Do I need to upgrade to a higher version oa Flash? Can I do anything about this without going through the additional expense of upgrading to Flash 9 for example and action script 3.

    PLEASE HELP

    MUCH OBLIGED

    BOB HATRAK

  • Hi All !

    Can u please elaborate the function…..I’m stuck to the time out issue and tried using the above code. but :(

    Help is appreciated.
    Thanks

  • to Bob Hatrak…
    so, just setting a script-time-out to = (4-5 times * 15 seconds) doesn´t work?
    Well, if that doesn´t solve Your problem, re-organizing the logic of Your code could (and using as3 with it´s blazing fast vector native types) get You the kick Your app needs..if that´s not an option, try breaking large block of functions into pieces – as far i remember the script time out is per function call/return.

    to Nick…
    explaining Your problem would get You a LOT faster to a solution. If You´re trying out the example code from the post, obviously You ARE FORCING a script time out…It is not the solution, but the problem.
    To set the time out limit You have to use the Flash authoring tool´s option (like mentioned in the post), if You´re on a command line compiler (as mxmlc), see my post above.

    cheers.

  • To Bob,
    If you really want to compute something complicated, try Alchemy in Flash 10, it needs write C code to run in flash, and the side effect is the flash size will be a little larger.

Leave a Reply




-->