const animationClass = 'animate-number';
const animDuration = 1500;
const totalFrames = 100;
const updateEvery = animDuration / totalFrames;
const totalKeyframes = Math.floor(animDuration / updateEvery);
const easeOut = x => x * (2 - x);
const animateNumber = el => {
let keyframe = 0;
const finalNumber = parseInt(el.innerHTML, 10);
const counter = setInterval(() => {
keyframe++;
let progress = easeOut(keyframe / totalKeyframes);
let currentNumber = Math.round(finalNumber * progress);
if (parseInt(el.innerHTML, 10) !== currentNumber) {
el.innerHTML = numberCommaSeperator(currentNumber);
}
if (keyframe === totalKeyframes) {
clearInterval(counter);
}
}, updateEvery);
}
const numberCommaSeperator = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
document.querySelectorAll(`.${animationClass}`).forEach(animateNumber);