<pre id="cont">0</pre>
<button id="btnStart">start</button>
<pre id="cont2">0</pre>
<button id="btnStart2">start</button>
<script>
btnStart.onclick = function(){
autoCounter('#cont', 0, 10050, 12.7);
};
btnStart2.onclick = function(){
autoCounter('#cont2', 100500, 200000, 300);
};
function autoCounter(selector, from, to, step) {
const container = document.querySelector(selector);
let currValue = from;
tick();
function tick() {
if (currValue < to) {
currValue += step;
if (currValue > to) {
currValue = to;
}
container.textContent = Math.round(currValue);
window.requestAnimationFrame(tick);
}
}
}
</script>