开发者

Does javascript have a maximum runtime?

I'm running a loop in javascript that should take about a minute to complete. When I c开发者_开发知识库lick 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);
}


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


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.


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.


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜