Disabling the Long-Running-Script Message in Internet Explorer

Disabling the long-running-script message in Internet Explorer

This message displays when Internet Explorer reaches the maximum number of synchronous instructions for a piece of JavaScript. The default maximum is 5,000,000 instructions, you can increase this number on a single machine by editing the registry.

Internet Explorer now 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, for the current page with the script engine. Internet Explorer displays a "long-running script" dialog box when that value is over a threshold amount.

The only way to solve the problem for all users that might be viewing your page is to break up the number of iterations your loop performs using timers, or refactor your code so that it doesn't need to process as many instructions.

Breaking up a loop with timers is relatively straightforward:

var i=0;
(function () {
for (; i < 6000000; i++) {
/*
Normal processing here
*/

// Every 100,000 iterations, take a break
if ( i > 0 && i % 100000 == 0) {
// Manually increment `i` because we break
i++;
// Set a timer for the next iteration
window.setTimeout(arguments.callee);
break;
}
}
})();

How to prevent Stop running this Script in browsers?

The problem is javascript is single-threaded, so if there is a function that takes too long to complete, this function could cause the UI not responding. Therefore, the browser will warn the user about long running script by displaying the message: "Stop running this script". The solutions to this problem are:

  • Optimize your code so the function does not take so long.
  • Use setTimeout to break the function execution into many pieces that are short enough.

Example code pattern:

var array = []; //assume that this is a very big array
var divideInto = 4;
var chunkSize = rowCount/divideInto;
var iteration = 0;

setTimeout(function doStuff(){
var base = chunkSize * iteration;
var To = Math.min(base+chunkSize,array.length);
while (base < To ){
//process array[base]
base++;
}
iteration++;
if (iteration < divideInto)
setTimeout(doStuff,0); //schedule for next phase
},0);

The solution you take in your Example(2) is correct, but there is a problem in your code. That's the setTimeout does not run. Try changing your code like this:

RepeatingOperation = function(op, yieldEveryIteration) 
{
var count = 0;
var instance = this;
this.step = function(args)
{
op(args);
if (++count <= yieldEveryIteration)
{
setTimeout(function() { instance.step(args); }, 1, [])
}
};
};

Modify your function ApplyNoFindings(), try this:

if (++i > iTotalRecords) 
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}

instead of:

if (++i < iTotalRecords) 
{
ro.step();
}
else
{
UpdateSummaryLine();
UpdateSummaryLineReasonCode();
}

Note: not tested, just give you an idea how it should work

Block stop execution of this script message

For IE versions 4 to 8, you can do this in the registry. Open the following key in Regedit: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles. If this key doesn't exist, create it. In this key, create a DWORD value called MaxScriptStatements and give it the value 0xFFFFFFFF (don't worry if the value changes automatically when you click on OK, that's normal).

You can program JavaScript to do this automatically:

var ws = new ActiveXObject("WScript.Shell");
ws.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Styles\\","");
ws.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Styles\\MaxScriptStatements",1107296255,"REG_DWORD");

a script on this page is causing ie to run slowly

Get yourself a copy of the IBM Page Profiler:

https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=61d74777-1701-4014-bfc0-96067ed50156

It's free (always a win). Start it up in the background, give it a few seconds, then refresh the page in IE. Go back to the profiler and it will list out all the resources used on the page and give you detailed profile information - in particular where JavaScript is taking a long time to execute.

It should be a good start to finding the source of your problem.

If the script tags are inline, I'd suggest creating a local copy of the file and separating out the script tags to separate files if you can.

Stop running this script

Someone came up with an idea here

Avoiding the 'Script taking too long' (all browsers have some form or another of this) message in browsers is relatively simple. You just have to make sure the browser knows you have not created an endless loop or recursion. And the easiest way to do is is to just give the browser a breather in between long running tasks.

So how would we go about this. Well the simplest solution is to just break up your task into mutliple smaller tasks with a setTimeout in between these tasks. The utility (see link) does just this

How to suppress the 'stop running this script' prompt in the c# WebBrowser control?

You're seeing that message because Javascript is waiting for the function to return, and execution is sitting in the C# side during your lengthy operation.

When the user hits the button, have C# kick off the operation using Dispatcher.BeginInvoke (msdn) or a BackgroundWorker (msdn). While the operation is running, execution can return to the Javascript. Then have the C# tell the Javascript when the job is done.



Related Topics



Leave a reply



Submit