0

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();
JosEvora
  • 134
  • 1
  • 9

1 Answers1

1

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

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Ok, i see what you mean... so it wont give that 'script not responding' message... My Script doesn't give me that! it never did that, not a single time! Like i said, it runs, does everything... but if i set it to run more than 15 or 20 times... it just stops! Without giving any message, warning or error! (i know it doesn't keep on running because the page doesn't change! If it was running, i would see 'movement' on the page!) – JosEvora Dec 10 '13 at 01:09
  • 1
    @JosEvora - Which browser are you seeing this in? When you say it "stops", what do you mean exactly? Stops executing your script, but the page is live and you can interact with it? Browser hangs and becomes unresponsive? Are there any errors shown in the console? It is also possible that your script is consuming too much memory, but we'd have to see the whole code to comment on that. Have you looked at your CPU monitor to see if the CPU is still busy? – jfriend00 Dec 10 '13 at 01:12
  • Well, the page is responsive! Just no script running! The console doesn't show errors... i'm using **FireFox 25.0.1**, and running the script from **FireBug 1.12.5**! The resources on the computer are always far from being 'overused'... everything is green! is like the browser just ignores the script after running 15 or 20 times... :\ – JosEvora Dec 10 '13 at 01:19
  • 1
    @JosEvora - I'm more familiar with the "unresponsive script" message in Chrome. Maybe Firefox just stops running it after too long or for some other reason. I'd suggest that you change the script to do work into smaller chunks separated by a timer as the various links I've provided in my answer show and see if that solves your issue? I've run things for a very long time that way. – jfriend00 Dec 10 '13 at 01:24
  • 1
    @JosEvora - it also appears that the unresponsive script message in Firefox could have been turned off in your browser and that the max run time can be configured. See [this article](http://kb.mozillazine.org/Dom.max_script_run_time) for details. – jfriend00 Dec 10 '13 at 01:32
  • I ended up changing the code... the new code was becoming unresponsive, so i used your explanation to go around it! Now it works fine! Thanks! ;) – JosEvora Dec 11 '13 at 10:09