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,
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?
just some syntax errors... http://jsfiddle.net/Detect/HnpCr/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With