1502 A script has executed for longer than the default timeout period of 15 seconds.

A script executed after the timeout period. (The default timeout period is 15 seconds.) After this error occurs, the script can continue to execute for 15 seconds more, after which the script terminates and throws run-time error number 1503 (A script failed to exit after 30 seconds and was terminated.)

Problem:
You have entered the realm of “Flash Infinite Loop” or at least a really long loop. This is very common when running loops, especially do…while loops. The timeout is now set to 15 seconds

Fix:
Make sure that you are not executing a loop that will never break out. Those poor loops running in circles for all of us Flashers/Flexers.

*******************Warning*******************
The Bad Code below will send your Flex/Flash into and infinite loop and will potentially crash your Development Tool. DO NOT COPY and PASTE the BAD CODE EXAMPLES BELOW.

Bad Code:
[as]
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i–);
trace(i);
}
[/as]
Good Code:
[as]
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i++);
trace(i);
}
[/as]
The code examples above have the var i starting with a number that is smaller than the number it is trying to iterate to. This code basically says as long as i is smaller than myLength then loop and each time I loop make i even smaller.

Bad Code:
[as]*********This code crashed my Flash multiple times.*******
var myArray:Array = [“joe”, “bob”,”pam”];
var randNum:int = Math.round(Math.random()*myArray.length-1);
var curNum:int = 1;
do {
trace(randNum);
} while (randNum == curNum);
[/as]
The key to using do…while loops in Flash is to make sure that the variable that you are validating against is changed inside the loop itself. Unless the variable that you are validating against changes within the loop itself it will loop forever. This example of a do…while loop is valid but will very likely put Flash into an infinite loop because the likelihood that the number will be 1 is very high, especially considering that the code is rounding and the number 1 is the middle digit of the three. This loop can execute quickly if Flash happens to choose another number or it can loop infinitely. The reason you might be doing this kind of loop is because you want to pull things out of an array, xml, or some other list and don’t want to get duplicates. A better way to do this is to either use the splice() methods in Flash or Flex or if you don’t want to affect the original array you can slice() out a duplicate without the curNum value and then the value will never be repeated because it no longer exists in the items that you are evaluating against. That way you know for sure the item that you don’t want repeated is gone.

There are many other ways to run into an infinite loop. These are just a couple. Feel free to post any questions you have in the comments section and I will answer them promptly.

Advertisements

8 thoughts on “1502 A script has executed for longer than the default timeout period of 15 seconds.

  1. I get that error with this code:
    var pressed_down_button:Boolean = false;
    public function pro():void
    {
    for (var i:int = 0; i

    How can I fix the problem?

    Thanks!

    Like

  2. In my flex app, this actionscript function is called on form submission for validation:


    if (strDt.text == “” || strDt.text == ” “) {
    strDt.errorString = “Enter the report date here”;
    strDt.setFocus();

    // Display error to user by calling js function
    errmsg = “ERROR: You need to enter the report date.”;
    ExternalInterface.call(”swfAlerts”, errmsg);
    return;
    }

    The javascript function swfAlerts calls the browsers alert message box and displays the error message:

    function swfAlerts(usrmsg) {
    alert(usrmsg);
    }

    Everything works successfully, and flex executes the swfAlerts function if the required text field is empty. But when the browser alert message box pops up, if the user doesn’t click the “OK” button to dismiss the message box within 15 seconds, flash gives the #1502 error:

    Error: Error #1502: A script has executed for longer than the default timeout period of 15 seconds.
    at flash.external::ExternalInterface$/flash.external:ExternalInterface::_toAS()
    at flash.external::ExternalInterface$/call()
    at date/::validateSubmit()
    at date/___Button1_click()

    Any idea how this can be avoided?

    Like

  3. Sam,

    I would suggest (without seeing the rest of your code) that it has nothing to do with this small snippet or the javascript alert popping up. I am willing to wager that you either have another script that is executed at the same time or that the culprit is wating for further instructions from the javascript. This is a guess because I can’t see the script but if the error doesn’t pop-up if you click “OK” then the click somehow interrupts the already looping script that would have given you the error.

    Check all of your loops. ‘While’ loops are serious rogues when it comes to this error. Please let me know if you find the solution or what else I can do to help you.

    Thanks,

    Curtis J. Morley

    Like

  4. […] ActionScript Error Description: I love this ActionScript Warning. This is not technically an ActionScript Error instead it is a warning that helps you code better. It is very clear and easily deciphered. This states that you should not use the old way to add text to a text field but rather use the new TextField.appendText() method. This improves performance dramatically and will prevent the user from getting the 15 sec. timeout. (Error #1502: A script has executed for longer than the default timeout period of 15 seconds.) […]

    Like

  5. Error #1502: A script has executed for longer than the default timeout period of 15 seconds

    hai i am using Httpservice to fetch the records through xml generated by JSP, my problem is when i get thousands of records the operation creates errors like the abouve!!!
    What should i do to to avoid getting such error???
    I am binding the result to a dataGrid…

    Like

  6. Kuhan,

    You may either want to rethink the downloading of so many records or, you may want to use Flash Remoting. Remoting can dramatically speed up the Server execution time, transfer time, the parsing time, and display/render time of the data. You can really see the difference at this website. http://www.jamesward.org/census/

    Thanks,

    Curtis J. Morley

    Like

  7. I am using webservice with flex application to get the result (which i am getting in the e.message.toString() through Alert.show() method). After that it gives me the Error: 1502.
    instead of going into the MyFunction_result()
    it goes to MyFunction_fault() method
    message shown in the Alert.show() is given below:
    ———————————-

    (mx.messaging.messages::AcknowledgeMessage)#0
    body = ”

    1823
    Sample
    UserNone
    Muser@sample.com
    000
    Abkhazia


    clientId = “DirectHTTPChannel0″
    correlationId = “AF002F25-B40D-D1C6-7B00-5DBC7079D6D2″
    destination = “”
    headers = (Object)#1
    messageId = “250505C5-D00E-9186-6A34-5DBD5DC79C30″
    timestamp = 0
    timeToLive = 0

    ——————————————
    and the flex code is here:

    {NetworkID}

    public function MyFunction_Fault(e:FaultEvent):void
    {
    Alert.show(e.fault.message.toString(), “Message”);
    }

    public function MyFunction():void
    {
    this.MyWebservice.MyFunction.send();
    // this is working fine and sending me the result
    // i am able to get the result in the Alert.show(e.message.toString())
    // but this function is unable to move to MyFunction_Result()
    // instead of showing me “result” in alert message
    // it is showing me the error in the alert message
    }

    private function MyFunction_Result(e:ResultEvent):void
    {
    Alert.show(”Reults”);
    }

    Thanks,

    Jatin Sahotra

    Like

  8. Hey! Great Stuff.

    This is the code that was killing my script:

    var index = String(event.target.name);
    while (index != String(uint(index))){
    index = index.substr(2);
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s