Tag Archive for 'error'

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.