core/skin/p007.yurecnt.ru/js/counter.js

25 lines
650 B
JavaScript
Raw Normal View History

2022-12-11 15:29:37 +05:00
//Counter
document.addEventListener("DOMContentLoaded", () => {
function counter(id, start, end, duration) {
let obj = document.getElementById(id),
current = start,
range = end - start,
increment = end > start ? 1 : -1,
step = Math.abs(Math.floor(duration / range)),
timer = setInterval(() => {
current += increment;
obj.textContent = current;
if (current == end) {
clearInterval(timer);
}
}, step);
}
counter("count1", 0, 253, 3000);
counter("count2", 100, 252, 2500);
counter("count3", 0, 1252, 3000);
counter("count4", 0, 52, 3000);
});