title: Sleep inside of setInterval without freezing the browser
body: <p>I have a function called "loop" which is being called repetitively using setInterval(). Inside of that function I need another function which has to be called "delay()" (it has to be the exact syntax).  </p>
  
  <p>So I ended up with this:</p>
  
  <pre><code>function loop() {
      console.log("some code")
      delay(100)
      console.log("some more code")
      delay(100)
  }
  
  
  
  if (typeof loop != "undefined")
      window.loopInterval = setInterval(loop, 1)
  
  function delay(millis) {
      var now = Date.now();
      while(Date.now() &lt; now + millis){} 
  }
  </code></pre>
  
  <p>This works just fine, but it freezes the browser making it unable to render certain changes properly.<br>
  Now is there any way I could change the delay function so that it simply temporarely pauses the setInterval?  </p>
  
  <p>As I said the syntax with the loop and delay function has to remain intact. And yes I have good reasons for that.  </p>
  
  <p><strong>For all those who didn't believe me that I do in fact have a very good reason, here you go: <a href="https://github.com/T-vK/LedStripSimulator" rel="nofollow">https://github.com/T-vK/LedStripSimulator</a><br>
  Code written for Arduinos almost always uses the loop() function and delay() is also a very common thing on Arduinos as they often only do one thing at a time anyways. And a JS simulator whose purpose it is to replicate the Arduino syntax as accurately as possible certainly needs a synchronous delay function.</strong></p>