2

I'm building a js library that reads binary files, including zip files.

Because there's no direct native support for arrays of binary data, when the zip files get large, there's a lot of copying that has to go on (See my other question for details).

This results in a "Stop Running this script?" alert. Now, I know this can happen if there's an infinite loop, but in my case, it's not an infinite loop. It's not a bug in the program. It just takes a loooooong time.

How can I suppress this?

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 1
    Why are reading ZIP files in Javascript? – SLaks Dec 18 '09 at 01:06
  • 2
    Why read zip files in Javascript? Because I can. I wanted to see if it were possible, and so I'm building it. I can imagine some possible applications. Not sure how practical it will be. For now, it works, although for a 145kb file, it takes several seconds to unzip/uncompress. – Cheeso Dec 18 '09 at 01:36

5 Answers5

4

This message is for security reason enabled, because otherwise you could simply block the users browser with a simple never ending loop. I think there no way to deactivate it.

Think about splitting you processing into serval parts, and schedule them via setTimeout, this should surpress the message, because the script is now not running all the time.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
3

You could divide the process into increments, then use setTimeout to add a small delay.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    A delay isn't neccesary — even a timeout of zero will allow a return to the event loop and should reset the warning. – s4y Dec 18 '09 at 01:22
2

In IE (and maybe Firefox too), the message is based on the number of statements executed, not the running time. If you can split some of the processing into a separate function, and defer it with setTimeout, I believe that it won't count toward the limit.

JW.
  • 50,691
  • 36
  • 115
  • 143
2

...answering my own question, so I could post the code I used.

The main issue was, I was reading the entire contents of a file, with a readToEnd() method, which actually reads one byte at a time. When reading a large file, it took a looooong time. The solution was to read asynchronously, and in batches.

This is the code I used:

readToEndAsync : function(callback) {
    _state = "";
    var slarge = "";
    var s = "";
    var txtrdr = this;

    var readBatchAsync = function() {
        var c = 0;
        var ch = txtrdr.readChar();
        while(ch != null) 
        {
            s += ch;c++;
            if(c > 1024) 
            {
                slarge += s;
                s = "";
                break;
            }
            ch = txtrdr.readChar();
        }
        if (ch!=null){
            setTimeout(readBatchAsync, 2);
        }
        else {
            callback(slarge+s);
        }
    };

    // kickoff
    readBatchAsync();
    return null;
},

And to call it:

textReader.readToEndAsync(function(out){
     say("The callback is complete");
     // the content is in "out"
});
Cheeso
  • 189,189
  • 101
  • 473
  • 713
0

I believe this feature is specific to Firefox and/or other browsers, and it has nothing to do with the javascript language itself.

As far as I know you (the programmer) have no way of stopping it in your visitors' browser.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156