title: How to loop a Web Worker?
body: <p>I'm making a website where using PHP, AJAX and Javascript the user can help to calculate PI, this is purely for learning purposes and I'm know there are more efficient ways to calculate PI. </p>
  
  <p>I need to pick a random location and then look if it's in a circle, there for I have created this code</p>
  
  <pre><code>var size = 500;
  
  function calculate() {
      var Xpos = Math.floor(Math.random() * size);
      var Ypos = Math.floor(Math.random() * size);
  
      var dist = Math.hypot(Ypos-Xpos, size / 2 - size / 2);
  
      if (dist &lt;= size / 2) postMessage(true);
      else postMessage(false);
  
      setTimeout("calculate()", 1);
  }
  
  calculate();
  </code></pre>
  
  <p>Which works great, altough it could be faster since there's a unneeded timeout in there (I think its uneeded anyway), so If I just remove it and replace it with only <code>calculate();</code> to speed it up I get this error</p>
  
  <blockquote>
    <p>Uncaught RangeError: Maximum call stack size exceeded</p>
  </blockquote>
  
  <p>How do I solve this, I would like to keep the worker working for every (or until the user closes the page)</p>
  
  <p>EDIT: 
  To be clear, the code I posted above works! but it stops working when I replace <code>setTimeout("calculate()", 1);</code> with <code>calculate();</code></p>