JSFiddle - React, Tailwind, and code Playground
by mcsf
HTML
<h1 id="counter">60</h1>
JavaScript
const length = 500
const final = 28
let start, initial, delta
function step(timestamp) {
if (! start) {
start = timestamp
initial = Number(counter.innerText)
delta = initial - final
}
var progress = timestamp - start
counter.innerText = final +
Math.max(0,
Math.round(
(1 - (progress / length)) * delta)
);
if (progress < length) {
window.requestAnimationFrame(step)
}
}
window.requestAnimationFrame(step)