SoFunction
Updated on 2025-04-09

js learn to use setTimeout to implement round-robin animation

This article shares the specific code of setTimeout to implement the round-robin animation for your reference. The specific content is as follows

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div id='box'></div>
  <script>
    var oBox = ("box");
    var maxLeft = ('clientWidth')-;
    var step = 5;
    var timer = null;
    //Use recursive idea to complete the round-robin animation of setTimeout: every time you clear the useless timer that was set before executing the animation, saving our memory space    function move(){
      (timer);
      var curLeft = (oBox,"left");
      if(curLeft+step >= maxLeft){//Border judgment        (oBox,"left",maxLeft);
        return;
      }
      curLeft+=step;
      (oBox,"left",curLeft);
      timer = (move,10)
    }
    move();

  </script>
</body>
</html>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.