title: How to keep the arrow position unchanged when restart it using requestAnimationFrame()?
body: <p>I tried to understand an <a href="https://mdn.github.io/learning-area/javascript/asynchronous/loops-and-intervals/start-and-stop-spinner.html" rel="nofollow noreferrer">example</a> on MDN.</p>
  <p>The effect of the <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/loops-and-intervals/start-and-stop-spinner.html" rel="nofollow noreferrer">current code</a> is a bit confusing to me - after a pause if you wait a while and start again, the position of the arrow is not where it originally stopped.</p>
  <p>How to make sure to restart where I left off? I thought of a way as below, but I am not very satisfied with it.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const spinner = document.querySelector('div')
  let rotateCount = 0
  let startTime = null
  let rAF
  let spinning = false
  let previousRotateCount = 0
  let hasStopped = false
  let rotateInterval
  
  function draw(timestamp) {
    if (hasStopped) {
      // The angle of each rotation is constant
      rotateCount += rotateInterval
      rotateCount %= 360
    } else {
      if (!startTime) {
        startTime = timestamp
      }
      rotateCount = (timestamp - startTime) / 3
      rotateCount %= 360
      rotateInterval = rotateCount - previousRotateCount
      previousRotateCount = rotateCount
    }
    console.log(rotateCount)
    spinner.style.transform = `rotate(${rotateCount}deg)`
  
    rAF = requestAnimationFrame(draw)
  }
  
  document.body.addEventListener('click', () =&gt; {
    if (spinning) {
      hasStopped = true
      cancelAnimationFrame(rAF)
    } else {
      draw()
    }
    spinning = !spinning
  })</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>html {
    background-color: white;
    height: 100%;
  }
  
  body {
    height: inherit;
    background-color: #f00;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  div {
    display: inline-block;
    font-size: 10rem;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div&gt;↻&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>I hope there is a better way. Thank you very much.</p>