Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent the slow script warning and force the browser to continue running script until it's finished?

Tags:

javascript


UPDATE (5 July 2013):

I've learned much since I originally asked this question. In one of the comments below it was suggested that I re-approach the task and find a way to solve it without risk of blocking the UI. I said it was impossible, the function must run as is. I don't actually remember what I was trying to accomplish with the function, but I'm certain that the commenter was right, and I was wrong. If you stumble upon this question and are in a similar situation as I was, then consider very seriously the possibility that your approach is flawed.

You might also want to look into web workers.

Here is where you can expect them to work.


Original Question:

Rather than get overly specific to my problem, I'll keep this generic so other people who stumble upon this can find it useful.

I have a function that accepts one parameter, a string. If the string is short the function runs fine and completes in a timely fashion. If, however, the string that's passed in is too long the script runs for a bit, then eventually times out and returns the browser's slow script dialog that allows the user to kill the script.

I would like to prevent that from happening so the script can continue to its terminus.

How can this be done?

A side note: If I can get this working, I would also like to make a status bar, similar to the one you see when loading gmail, appear so the user knows things are happening.

Thanks!

like image 416
Tyler Biscoe Avatar asked Jul 06 '11 14:07

Tyler Biscoe


People also ask

How do I stop scripts from running?

A script on this page may be busy or may have stopped responding. You have the choice of "Stop Script" or "Continue" buttons. Click the "Stop Script" button to stop the script from running. Stopping the script can prevent the browser from running out of memory or crashing.


2 Answers

You can try not blocking the UI completely and make it run "in another thread" with setTimeout.

window.setTimeout(function() { ... code ... }, 0);

I say "in another thread" because there is no real multi-thread in JS, and here is an nice article about it


EDIT: (To make this answer more up-to-date with web technologies)

You can also use HTML 5 Web Workers depending on the browser you are targeting

like image 110
Felipe Sabino Avatar answered Oct 18 '22 23:10

Felipe Sabino


No way you can disable that behavior in any browser by script. Imagine the impact, disable that feature and call a while(1). Pretty bad.

It's on you to create code which does not block the browser for "too long". There are a ton if best-practice patterns. I always recommend to try to break down your algorythm into smaller chunks. Combine that with a runaway-timer and a loop which makes sure that any operation does not run for longer than 100ms (for instance).

var sequence = ['foo', 'bar', 'baz', 'base', 'ball', 'hello', 'world', '100k more'],
    start = Date.now();

setTimeout(function _worker() {
    do {
       var element = sequence.shift();
       // do something with element
    } while( sequence.length && (Date.now() - start < 100) );

    if( sequence.length )
        setTimeout(_worker, 25);
}, 25);
like image 28
jAndy Avatar answered Oct 18 '22 21:10

jAndy