counter
Creates a counter with the specified range, step and duration for the specified selector.
- Check if
stephas the proper sign and change it accordingly. - Use
setInterval()in combination withMath.abs()andMath.floor()to calculate the time between each new text draw. - Use
Document.querySelector(),Element.innerHTMLto update the value of the selected element. - Omit the fourth argument,
step, to use a default step of1. - Omit the fifth argument,
duration, to use a default duration of2000ms.
const counter = (selector, start, end, step = 1, duration = 2000) => {
let current = start,
_step = (end - start) * step < 0 ? -step : step,
timer = setInterval(() => {
current += _step;
document.querySelector(selector).innerHTML = current;
if (current >= end) document.querySelector(selector).innerHTML = end;
if (current >= end) clearInterval(timer);
}, Math.abs(Math.floor(duration / (end - start))));
return timer;
};