I have some Javascript code that is very process intensive and it is triggering the “unresponsive script” warning. Each of the coded steps have to occur in the order in which they are coded. I think I have located the offending function but I don’t understand how to make it work without triggering the warning.
I found an indication of something that might help here (setTimeout) Javascript: Unresponsive script error but this was really vague so I kept looking. Here is a much better example but I cant see a way to implement this in my code.. How can I give control back (briefly) to the browser during intensive JavaScript processing? The original article http://www.julienlecomte.net/blog/2007/10/28/ is by all accounts genius but I cant seem to figure it for implementation here.
Here is the code I believe is causing the fit.
// -----------------------------------
// Output stats for all nations
// by continent in a colon delimited
// list further delimited by a ~
// ( ContinentName:NationPower:NationHTML )
// expects the output from the
// continent_stats function
// -----------------------------------
function Nations(ContinentData) {
document.write("<tr><th>Nation Stats</th></tr><tr>"); // setup progress bar
var NationString = ""; // init the string
var Carray = ContinentData.split("*"); //continents
for (cit = 0; cit < Carray.length; cit++) { // go through the continents
var Cstat = Carray[cit].split(":"); // make an array of the individual continent stats
var NumberOfNations = Cstat[4]; // nation #
var ContinentName1 = Cstat[0]; // Continent Name
document.write("<td class='ProgressBarText'>" + ContinentName1 + "</td><td class='ProgressBars'>"); // Format loader screen text
for (nnum = 0; nnum < NumberOfNations; nnum++) { // go through the number of nations on the continent
var nat1 = BuildCulture(); // build the nation
var Natname= "Nation"+padLeft(nnum,2,"0"); // name the nation
NationString = NationString + ContinentName1 + ":" + Natname + ":" + nat1 + "~"; // build the string
document.write("█"); // add to progress bar
}
document.write("</td><td>"+NumberOfNations+ " Nations</td></tr>"); // progress bar complete
}
document.write("</table>"); // end the loader screen table
// send the nation string back
return NationString;
}
So you can see that it cycles through continents and creates nations for each continent. The BuildCulture() function is the culprit. By itself it works just fine but string 8 or 9 together over the course of around 4 continents and the warning goes off.
I have tried using
setTimeout( function() { BuildCulture(); }, 1000);
all over the place, in the main code section, in the BuildCulture() function beginning and end, in the Nations(ContinentData) function in and out of the loops. It never works.
I am clearly looping too much but I need every loop. Will SetTimeout help me at all or am I chasing the wrong statement?
If SetTimeout is my target for a solution, how can I implement it in this code?
Thanks much.
P.S. I am only aiming for this to work in Firefox so compatibility with IE core browsers is not necessary.