2

I have an array with 2000 arrays which each have 2000 values (to stock a 2000x2000 image) in javascript in an HTA. I have this code to test how many "blue" values there are in the array (the array's name is image):

var bluePixels = 0;
for(i = 0; i < image.length; i++){
     for(j = 0; j < image[i].length; j++){
          if(image[i][j] == "blue"){bluePixels = bluePixels + 1}
     }
}
alert(bluePixels);

The problem is that it shows a message where it says something like "Stop execution of this script? A script on this page is slowing down Internet Explorer. If it continues, your computer might not answer." (I'm not sure of the exact words because my computer is in French) and then, if I click on "no", it does the alert(bluePixels) like it should but if I push on "yes" it doesn't. How can I block this message? (I know that there are workarounds like telling the user in the beginning to press "no" but I want a real solution)

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

3 Answers3

2

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");
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
1

Seems like you have an answer here

From the answer: "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."

So the first approach can be attained using a timeout for each such large iteration.

Community
  • 1
  • 1
Tom Teman
  • 1,975
  • 3
  • 28
  • 43
1

You need to split the 2000x2000 for-loop's up in smaller pieces of code, eg threads or processes, so the browsers maximum execution time not is becoming exhausted. Here the image array is parsed for one row at a time, controlled by a timer :

var bluePixels = 0,
    timer,
    i = 0;

function countBluePixels() {
    for (var j = 0; j < image[i].length; j++){
        if (image[i][j] == "blue") {
            bluePixels = bluePixels + 1;
        }
    }
    i=i+1;
    if (i>image.length) {
        clearInterval(timer);
        alert(bluePixels);
    }
}

timer = window.setInterval(countBluePixels, 0);

The code is the same, just splitted up in 2000 processes instead of 1.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • It's a good idea, but it doesn't want me to do `setInterval(...,0)`. I tried to do `setInterval(...,1)` and it worked but it took a lot of time so I preferred using Tom Teman's solution. – Donald Duck Jan 25 '15 at 14:46
  • @DonaldDuck You are using internet explorer? `4.4`, or `5` ms is said to be the lowest in practice, but `0` should just fire the processes off. Javascript is asynchronous. At lleast what I know of. Do you have 0 or "0"? "xxxx" would be bad, like "123", since the engine need to parse on every loop then. – davidkonrad Jan 26 '15 at 00:57
  • I wrote in the question that i'm using HTA, and HTA acts like IE7. I have `0` and not `"0"` and what it does is that it does the `countBluePixels()` function only once. – Donald Duck Jan 26 '15 at 08:42