7

I need to run a super long JavaScript on my page. The client is complaining that IE shows a warning dialog for the script being too long. Unfortunately, there is no way we can reduce the length of the script, so I am trying to find a bypass for the problem.

According to Microsoft support website:

IE tracks the total number of executed script statements and resets the value each time that a new script execution is started, such as from a timeout or from an event handler. It displays a "long-running script" dialog box when that value is over a threshold amount.

However I have tried to use both setInterval() and setTimeout() to break my script into pieces, but none is working. The browser I am using is IE8. My code is as following:

<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
</head>
<body>
    <div id ="test"></div>
    <div id ="log"></div>
</body>
<script>
    var repeat =0;

    function heavyTask(){
        if (repeat<50){
            y = longRun();
            setTimeout("heavyTask()",100);
        }else{
            $('#test').html("done!");
        }
    }
    function longRun(){
        for(var i =0; i<20000;i++){ }
        repeat++;
        $('#log').append('<div>repeat: '+ repeat +'</div>');
    };

    $(document).ready(function () {
        setTimeout("heavyTask()",100);
    });
</script></html>

In order to make the code work, you have to edit Registry, go to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles, and set the DWORD value called "MaxScriptStatements" to 100,000. If the Styles key is not present, create a new key that is called Styles.

Thanks,

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
  • Is there a question in there somewhere? – Marc B Mar 02 '11 at 20:21
  • @Marc Thanks for your quick response. I have searched for it first but I didn't find any feasible solution. Actually I think there might be no solution. I just want to make sure and report it to my boss. ;) – Xi 张熹 Mar 02 '11 at 20:23

2 Answers2

6

This processing limit is set by the browser, not JavaScript.

You can break your process down into smaller steps.

See this question: How can I give control back (briefly) to the browser during intensive JavaScript processing?

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Thanks for your response. Actually, your suggestion is what I have been trying to do. I just don't know why it's not working. The code in my post breaks down the process into smaller steps, but it seems that IE tracks the total # of statements regardless timeouts.:( – Xi 张熹 Mar 02 '11 at 20:29
  • 1
    Yeah, what you're doing makes sense. All I can suggest now is increasing the timeout before restarting the process. – Diodeus - James MacFarlane Mar 02 '11 at 20:53
  • 1
    Thanks you, that's the problem! After increasing the interval from 10 to 100, the script is working now. It's worth noting the way IE detecting long script. It "pulls" the result once a while to determine if there are too many statements have already executed. So it's possible that running more that MaxScriptStatements without seeing the warning. This's also why my testing script has been acting inconsistent. – Xi 张熹 Mar 02 '11 at 22:04
  • This could be related to the Timer Precision provided to IE by Windows. The lowest time precision it can handle is 15ms. So 10ms could be interpreted as either 0 or 15 resulting in unexpected behaviour. [Read John Resig on this topic](http://ejohn.org/blog/accuracy-of-javascript-time) – Uchitha Jul 28 '11 at 08:51
1

just some syntax errors... http://jsfiddle.net/Detect/HnpCr/3/

Detect
  • 2,049
  • 1
  • 12
  • 21
  • Thanks, the code looks much better... however it doesn't solve the problem. If you set the MaxScriptStatements to 100,000 and try to run the script in IE, you still get the same warning. :( – Xi 张熹 Mar 02 '11 at 20:41