mm_foley mm_foley - 1 year ago 120
jQuery Question

jQuery count to number with duration between digits

What I am looking to do is have a number start counting either up or down, lets say from 1 to a 100 or a 100 to 1. I have found many many plugins that do this but they all have a duration variable. The problem is that duration variable applies to how long it takes to get from start to finish. I am looking for a way to set how long it takes to get to the next digit. IE counting from X to Y and updating the number every .25 seconds. This way a larger number would take longer to get to than a smaller number.

This plugin looks great but I can't get it to do what I want.

Any help would be great.

Kld Kld
Answer Source

You can try something like this

function count(target, start, end, step, timeInterval) {
  var timer = setInterval(function() {
    if (start == end) {
      return false;
    start += step;
  }, timeInterval);

var myInterval = 150; //0.15 seconds
count($('#timer1'), 1, 100, 1, 50);
count($('#timer2'), 100, 1, -1, 50);
<script src=""></script>
<div id="timer1"></div>
<div id="timer2"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download