javascript主线程上的异步循环和间隔async loops and intervals


So that's it — all the essentials of async loops and intervals covered in one article. You'll find these methods useful in a lot of situations, but take care not to overuse them! Because they still run on the main thread, heavy and intensive callbacks (especially those that manipulate the DOM) can really slow down a page if you're not careful.
重的和intensive(密集的)callbacks(尤其是操作DOM)会让你的页面非常慢。主线程上的异步。
    <div class="spinner"><div><p>↻</p></div></div>

    <section class="ui">
      <div class="topbar">
        <p class="p1">Player 1: "A"</p>
        <p class="p2">Player 2: "L"</p>
        <div class="middlebar">
          <button>Start game</button>
          <p class="result"></p>
        </div>
      </div>
    </section>
    <script>

      // Store reference to the spinner and spinner container, create a rotate counter and null startTime
      // and create an uninitialized variable to store a requestAnimationFrame() call in,
      const spinner = document.querySelector('.spinner p');
      const spinnerContainer = document.querySelector('.spinner');
      let rotateCount = 0;
      let startTime = null;
      let rAF;

      // Store references to the start button and the result paragraph
      const btn = document.querySelector('button');
      const result = document.querySelector('.result');

      // function to generate random number
      function random(min,max) {
        var num = Math.floor(Math.random()*(max-min)) + min;
        return num;
      }

      // Create a draw() function
      function draw(timestamp) {
        if(!startTime) {
         startTime = timestamp;
        }

        rotateCount = (timestamp - startTime) / 3;
        
        // If rotateCount gets over 359, set it to 'remainder of dividing by 360'
        if(rotateCount > 359) {
          rotateCount %= 360;
        }

        // Set the rotation of the div to be equal to rotateCount degrees
        spinner.style.transform = 'rotate(' + rotateCount + 'deg)';

        // Call the next frame in the animation
        rAF = requestAnimationFrame(draw);
      }

      // Initially hide the spinner and results
      result.style.display = 'none';
      spinnerContainer.style.display = 'none';

      // Reset the game to its initial state on restart
      function reset() {
        btn.style.display = 'block';
        result.textContent = '';
        result.style.display = 'none';
      }

      // Start the game when the button is pressed

      btn.addEventListener('click', start);

      function start() {
        // Start the spinner spinning
        draw();
        // Show the spinner and hide the button
        spinnerContainer.style.display = 'block';
        btn.style.display = 'none';
        // run the setEndgame() function after a random number of seconds between 5 and 10
        setTimeout(setEndgame, random(5000,10000));
      }

      // Function to allow players to take their turn when the time is right
      function setEndgame() {
        cancelAnimationFrame(rAF);
        spinnerContainer.style.display = 'none';
        result.style.display = 'block';
        result.textContent = 'PLAYERS GO!!';

        document.addEventListener('keydown', keyHandler);

        function keyHandler(e) {
          let isOver = false;
          console.log(e.key);
          
          if (e.key === "a") {
            result.textContent = 'Player 1 won!!';
            isOver = true;
          } else if (e.key === "l") {
            result.textContent = 'Player 2 won!!';
            isOver = true;
          }

          if (isOver) {
            document.removeEventListener('keydown', keyHandler);
            setTimeout(reset, 5000);
          }
        };
      }
    </script>

阅读量: 542
发布于:
修改于: