Files
30-seconds-of-code/test/counter/counter.js
Angelos Chalaris 95a121a832 Add counter snippet
2018-05-06 17:55:46 +03:00

12 lines
447 B
JavaScript

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;
};
module.exports = counter;