title: Click during a while loop is not placed in event loop
body: <p>I am following <a href="https://www.youtube.com/watch?v=Bv_5Zv5c-Ts" rel="nofollow noreferrer">https://www.youtube.com/watch?v=Bv_5Zv5c-Ts</a>, and it is explained there that the event loop of JS engine places events in Event Queue - their handlers will be executed only when the Execution Stack is empty. The author even shows an example of it at 1:45:18. The code is:</p>
  <pre class="lang-js prettyprint-override"><code>function waitThreeSeconds() {
      var ms = 3000 + new Date().getTime();
      while(new Date() &lt; ms) {}
      console.log('finished function');
  }
  
  function clickHandler() {
      console.log('click event!')
  }
  
  document.addEventListener('click', clickHandler);
  
  waitThreeSeconds()
  console.log('finished execution')
  </code></pre>
  <p>When I run it in the browser, the while loop runs for 3 seconds as expected, and then the 2 messages get printed:</p>
  <p><a href="https://i.stack.imgur.com/kRvBA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kRvBA.png" alt="enter image description here" /></a></p>
  <p>If I click anywhere AFTER the while loop finishes, a message &quot;click event!&quot; gets printed. However, if I click DURING the while loop, the click event is never registered. In the video, in both situations, the click is registered.</p>
  <p>Is it due to some updates in the JavaScript engines that happened since the video's premiere in 2015? I'm using the MS Edge browser.</p>
  <p>The video's author suggests that even though JS is a single-threaded language, the web browsers implement JS interprets/engines in a concurrent way where the events get added to the Event Queue separately from the JS code's execution.</p>
  <p>My experiment confused me since it shows different behavior. Could someone explain why is that?</p>
  <p>//EDIT</p>
  <p>After further experimentation, I found out that the behavior seen in the video is also to be found in Chromium (and I guess Chrome as well, although I do not have it installed).
  Edge, however (the Chromium-based one), behaves differently and does not register the click at all during the while loop.</p>