microsoft / vscode-livepreview

Hosts a local server in your workspace for you to preview your webpages.
http://aka.ms/live-preview
MIT License
394 stars 58 forks source link

Deferred console.log output #602

Open gitspeaks opened 8 months ago

gitspeaks commented 8 months ago

Consider the following:

<!DOCTYPE html>
<html>
  <body>
    <button>click</button>
    <script>
      var button = document.getElementsByTagName("button")[0];
      button.onclick = function () {
        console.log("test");
        var d = Date.now();
        while (Date.now() < d + 10000) {}
      };
    </script>
  </body>
</html>

After clicking the button, both Chrome and Firefox immediately display 'test' in the developer console. In contrast, 'Live Preview' shows the string in the output pane after a 10-second delay. This behavior suggests that in 'Live Preview', console.log output is processed in the next event loop cycle, rather than in the current one as typically expected.

andreamah commented 7 months ago

In Live Preview, console.log will do some extra calls to show them in the Embedded Live Preview Console, so this is very possible. Curious: how exactly can this code tell which event loop cycle some code runs on?