title: Prevent JavaScript from locking up browser on big loop
body: <p>I have a loop which needs to be run 200 million times in a browser.  It's a simulator which several people need to use regularly.  It takes about 15 minutes to run, but during this time, the browsers will frequently pop up a warning with "this script is taking too long" etc., and it completely hangs Firefox during the function.  This also means the page does not update my status indicator (which is just a number).</p>
  
  <p>I have googled "javascript yield" and read the first 4 pages of hits.  Some discuss a new "yield" keyword, but there is only one description and example, which I find incomprehensible, e.g. "The function containing the yield keyword is a generator. When you call it, it's formal parameters are bound to actual arguments, but it's body isn't actually evaluated".  Does <code>yield</code> yield to the UI?</p>
  
  <p>One of the few solutions I did find is this old post which uses the deprecated callee argument and a timer to call itself:
  <a href="http://www.julienlecomte.net/blog/2007/10/28/" rel="nofollow noreferrer">http://www.julienlecomte.net/blog/2007/10/28/</a></p>
  
  <p>However, the above example doesn't contain any loop variables or state, and when I add these it falls apart, and my net result is always zero.</p>
  
  <p>It also doesn't do chunking, but I have found some other examples which chunk using "index % 100 == 0" on every iteration. However, this seems to be a slow way of doing it.  E.g. this:</p>
  
  <p><a href="https://stackoverflow.com/questions/714942/how-to-stop-intense-javascript-loop-from-freezing-the-browser">How to stop intense Javascript loop from freezing the browser</a></p>
  
  <p>But it doesn't have any way to update progress, and doesn't yield to the UI (so still hangs the browser).  Here is a test version which hangs the browser during execution:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;script&gt;
  var spins = 1000000
  var chunkSize = 1000;
  var chunk;
  function Stats() {this.a=0};
  var stats = new Stats();               
  var big;
  
  var index = 0;
  
  var process = function() {
    for (; index &lt; spins; index++) {
      stats.a++;
      big = (big/3.6)+ big * 1.3 * big / 2.1;
      console.write(big);
      // Perform xml processing
      if (index + 1 &lt; spins &amp;&amp; index % 100 == 0) {
          document.getElementById("result").innerHTML = stats.a;
          setTimeout(process, 5);
      }
    }
    document.getElementById("result").innerHTML = stats.a;
  };
  
  
  &lt;/script&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body  onload="process()"&gt;
  &lt;div id=result&gt;result goes here.&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and here is another attempt which the <code>stats.a</code> is always zero (So I presume there is some scoping issue):</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;script&gt;
  var spins = 1000000
  var chunkSize = 1000;
  var chunk;
  function Stats() {this.a=0};
  var stats = new Stats();               
  
  function doIt() {
      function spin() {
         for (spinIx=0; (spinIx&lt;chunkSize) &amp;&amp; (spinIx+chunk &lt; spins); spinIx++) {
             stats.a++;
         }
      }        
  
      for (chunk =0; chunk &lt; spins; chunk+=chunkSize){
          setTimeout(spin, 5);
              document.getElementById("result").innerHTML = stats.a;
          }
        document.getElementById("result").innerHTML = stats.a;
  }
  
  &lt;/script&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body  onload="doIt()"&gt;
  &lt;div id=result&gt;result goes here.&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I've spent 48 hours trying to get this working - either I am very dumb or this is very hard.  Any ideas?</p>
  
  <p>Several people have suggested web workers. I tried several days to get his working, but I could not find a similar example which passes a number etc.  The code below was my last attempt to get it working, but the result is always 0 when it should be 100000.  I.e. it fails in the same way that my second example above fails.</p>
  
  <p>spinMaster.html:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script&gt;
  if(typeof(Worker)==="undefined") {
    document.write("&lt;h1&gt;sorry, your browser doesnt support web workers, please use firefox, opera, chorme or safari&lt;/h1&gt;");
    } 
  var worker =new Worker("spinWorker.js");
  
  worker.postMessage({times:1000000});
  
  worker.onmessage=function(event){
  document.getElementById("result").innerHTML=event.data;
  }; 
  &lt;/script&gt;
  
  &lt;div id="result"&gt;result goes here&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>spinWorker.js</p>
  
  <pre><code>function State() {
      this.a=0;
  }
  
  var state = new State();
  
  self.addEventListener('message', spin, false);
  
  function spin(e) {
      var times, i;
  
      times = e.data.times;
  //times = 1000000; // this doesnt work either.
  
      for(i;i&lt;times;i++) {
          state.a++;
      }
  
      self.postMessage(state.a);
  }
  </code></pre>
  
  <p>resultant output: 0</p>