Advertisement
inodorotnuk

counter

Feb 11th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.70 KB | None | 0 0
  1. <script>
  2. let start // set on the first step to the timestamp provided
  3. const el = document.getElementById('count') // get the element
  4. const final = parseInt(el.textContent, 10) // parse out the final number
  5. const duration = 4000 // duration in ms
  6. const step = ts => {
  7.   if (!start) {
  8.     start = ts
  9.   }
  10.   // get the time passed as a fraction of total duration
  11.   let progress = (ts - start) / duration
  12.  
  13.   el.textContent = Math.floor(progress * final) // set the text
  14.   if (progress < 1) {
  15.    /*if we are not 100% complete, request another animation frame*/
  16.    requestAnimationFrame(step)
  17.  }
  18. }
  19.  
  20. /*start the animation*/
  21. requestAnimationFrame(step);
  22. </script>
  23.  
  24. <span id="count">200</span>%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement