JSFiddle - React, Tailwind, and code Playground

by maitrungduc1410

HTML

<!DOCTYPE html>
<h1 id="count">Count: 0</h1>
<button onclick="start()">Start</button>
<div id="log">Log:</div>
<script>
  "use strict";

  let count = 0;
  setInterval(() => {
    document.getElementById("count").innerHTML = `Count: ${++count}`;
  }, 1000);

  function start() {
    console.log("start");
    let i = 0;

    let start = Date.now();

    function count() {
      // move the scheduling to the beginning
      if (i < 2e9 - 1e6) {
        setTimeout(count); // schedule the new call
      }

      do {
        i++;
      } while (i % 1e6 != 0);

      if (i == 2e9) {
        document.getElementById("log").innerHTML =
          "Log: Done in " + (Date.now() - start) + "ms";
      }
    }

    count();
  }
</script>