title: JS setTimout confuse - Event Loop
body: <pre><code>var start = new Date;
  setTimeout(function(){
      var end = new Date;
      console.log('Time elapsed:', end - start, 'ms');   //Line4
  }, 500);
  while (new Date - start &lt; 1000) {};  //Line 6
  </code></pre>
  
  <p>I know that the output is "Time elapsed: 1002ms" and so on. Line 4 is executed after Line 6, so 'Time elapsed' should be larger than 1000.</p>
  
  <p>My confusion is that why 'Time elapsed' is 1500（1000+ 500）. There is 500ms delay in Line 4.</p>
  
  <p><a href="https://i.stack.imgur.com/uS6Pj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uS6Pj.png" alt="enter image description here"></a></p>