Javascript: Suppress “Stop running this script?”, or how to use setTimeout?

StackOverflow https://stackoverflow.com/questions/1925642

  •  20-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

...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"
});

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top