0
I have three counters, which go up to a certain number, set in data-count-to
of each tag, my problem here is that I would like them to reach their limit at the same time, even with values as different as is the case below:
function count_up(ele, count_to, timer, i) {
if(i > count_to) {
return;
}
ele.text(i.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1."));
i += 1;
/*console.log(timer);*/
setTimeout(function() {count_up(ele, count_to, timer, i)}, timer);
}
$('.counter-up').each(function() {
count_to = parseInt($(this).data('countTo'));
timer = parseInt(40000/count_to);
count_up($(this), count_to, timer, 0)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span class="counter-up" data-count-to="5684"></span>
</div>
<div>
<span class="counter-up" data-count-to="6603146"></span></div>
<div>
<span class="counter-up" data-count-to="20362"></span>
</div>
Now, I think my problem is math in this case, because I suspect I also have to tinker with the amount to increase as well, and not just in time.
I would like the end result to be only that the argument to be varied is time, but with any time that the three counters reach the end at the same time