user374343
user374343

Reputation:

Does javascript have a maximum runtime?

I'm running a loop in javascript that should take about a minute to complete. When I click the link that is supposed to activate the loop, the page makes no effort to load anything but when I comment the loop out, print statements work. Does javascript just know the process is going to take a while and not do it and if it does is there anyway to make it not do this? Thank you! PS here's the code (script array is over 60000 entries long):

function magic(charnumber) {
    var count = 1;
    alert(charnumber);
    var output = "";
    for (count; count < scriptAr.length; count += 4) {
        if (scriptAr[count] < charnumber and sriptAr[count + 1] > charnumber) {
            output = output + scriptAr[count + 2] + '\n';
        }
    }
    alert(charnumber);
}

Upvotes: 0

Views: 304

Answers (5)

beeglebug
beeglebug

Reputation: 3552

As other commenters have pointed out, it depends on the browser.

I'm not sure it will work in your instance, but one common way to get around the problem is to break the task down in to smaller chunks, and fire off a series of functions separated by calls to setTimeout() with a very small interval.

This has the effect of handing control back to the browser for a millisecond or two before going back to what you were doing, stopping it from throwing a wobbly about the script running for too long.

Upvotes: 0

heisenberg
heisenberg

Reputation: 9759

if(scriptAr[count]<charnumber and sriptAr[count+1]>charnumber)

That should be "&&" instead of "and"

Upvotes: 2

Alterscape
Alterscape

Reputation: 1546

I think AnthonyWJones is correct re: the && syntax error, above. Beyond that, what browser(s) does this need to work on? If you can limit support to modern browsers, you might look at WebWorkers, which I believe work on the latest Chrome, Safari, Firefox, etc. This is definitely a more user-friendly approach, since it won't block the GUI thread as your current code is likely to.

Upvotes: 2

Hooray Im Helping
Hooray Im Helping

Reputation: 5264

I believe this is a browser specific setting. In Firefox, you can change it by going to your address bar and typing about:config then searching for dom.max_script_run_time which represents how many seconds Firefox will try to let a script run before it tells you it's unresponsive.

Upvotes: 3

AnthonyWJones
AnthonyWJones

Reputation: 189495

I think its just failing because of the syntax error and should be &&.

Upvotes: 11

Related Questions