简体   繁体   中英

Is there a Time Limit for a JS script to run on the Browser Console or FireBug Console?

I'm using the firebug console to test a script... Right at the start of my script i have an option on the number of times i want the script to run!

My problem is:

  • The script stops after a while, without completing, if i put a big number!

notes:

  • If i put under 10 times, it runs always!
  • If i put 20 times, sometimes it runs 'till the end, some times it doesn't!
  • If i put over 20 times, it never ends properly!
  • Each time it runs, it takes between 1 to 6 minutes...
  • I checked the code, the logic seams ok
  • It runs! it does everything! just not more than 20 times... i need it to run much more than that :\\

example of code:

var x = prompt("How many times should this script be runned?");
alert("It will be runned " + x + " times then!");

function doThis() {
    setTimeout(function(){
        ...;
        ++n;
        --x;
        action();
    },65000);
}

function doThat() {
    setTimeout(function(){
        ...;
        ++n;
        --x;
        action();
    },65000);
}

function action() {
    if(x>0) {

        if(...) {
            if(n<6){                
            doThis();
            }
            } else  {
            if(n<6){                
            doThat();
            }
        }
    } else {
        alert("The Script has ended!");
    }

action();

Yes, most browsers today have a time limit on how long a script can run before it returns control back to the user. This is to prevent an errant or deviant script from "hanging" that browser window.

One can typically work around it by putting a smaller timer between runs. This gives the browser a chance to service it's event loop and prevents the prompt about running too long.

I shoot for a chunk of work taking no longer than 10 seconds or so and that is way below the threshold of all browsers. Here are some previous answers about breaking your work into chunks separated by a short timer interval so things can run basically forever.

Best way to iterate over an array without blocking the UI

Avoiding "Unresponsive Script" message in a foreach loop

Best way to iterate over an array without blocking the UI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM