-2

I want to have a function that runs every screen update in javascript a while (true) loop freezes the entire screen:

<!DOCTYPE html>
<html>
  <body>
    <p id="label">Something should display</p>
    <script>
      var l = document.getElementById("label");
      while (true){
        l.innerHTML = "Something";
      }
    </script>
  </body>
</html>

So is there a way to kind of do this???

AverseMoon
  • 11
  • 4

1 Answers1

0

You can have a function run on the next frame with window.requestAnimationFrame(callback)

<!DOCTYPE html>
<html>

<body>
  <p id="label">Loading</p>
  <script>
    var l = document.getElementById("label");

    function update() {
      l.innerHTML = Date.now();
      window.requestAnimationFrame(update);
    }
    update();
  </script>
</body>

</html>
Skylar
  • 928
  • 5
  • 18