I'm running a loop in javascript that should take about a minute to plete. When I click the link that is supposed to activate the loop, the page makes no effort to load anything but when I ment 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'm running a loop in javascript that should take about a minute to plete. When I click the link that is supposed to activate the loop, the page makes no effort to load anything but when I ment 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);
}
Share
Improve this question
edited Jul 8, 2010 at 15:52
gblazex
50.2k12 gold badges99 silver badges92 bronze badges
asked Jul 8, 2010 at 14:33
user374343user374343
2
- That would require solving the halting problem, so no. – Gabe Moothart Commented Jul 8, 2010 at 14:43
- Not necessarily, you could queue the alerts instead of displaying them right when the line is interpreted and then determine a good moment when to display them (ie memory usage/CPU usage is low). Disclaimer: I'm not saying this is how JavaScript handles alerts on most browsers but merely giving an example. – Waleed Amjad Commented Jul 8, 2010 at 15:57
5 Answers
Reset to default 11I 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 menters have pointed out, it depends on the browser.
I'm not sure it will work in your instance, but one mon 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.