function animateCounter(element, from, to, duration = 1000, easing = (t) => t * (2 - t)) {
const start = performance.now();
function update(now) {
const t = Math.min((now - start) / duration, 1);
element.textContent = Math.round(from + (to - from) * easing(t)).toLocaleString();
if (t < 1) requestAnimationFrame(update);
}
requestAnimationFrame(update);
}
// Usage
const el = document.querySelector('#counter');
animateCounter(el, 0, 1_234_567, 2000);
Create a free account and build your private vault. Share publicly whenever you want.