why jquery can't animate number accurately?

html, javascript, jquery

Solution

I have the same issue. The reasoning is because `animate` function uses a mathematical formula that is time based. You don't really notice this when animating something css based because close enough in pixels is good enough. It will get close to the final value but may not always be exactly the end value. Solution is to use the `complete` event to set that last value.

Here is what you need to do:

function animateNumber(ele,no,stepTime){
$({someValue: 0}).animate({someValue: no}, {
        duration: stepTime,
        step: function() { // called on every step. Update the element's text with value:
            ele.text(Math.floor(this.someValue+1));
        },
        complete : function(){
            ele.text(no);
        }
});
}

animateNumber($('#counterx'),100,10000);
animateNumber($('#countery'),100,1000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
counterx(slow): <span id=counterx>--</span>
<br/>
countery(fast): <span id=countery>--</span>

Problem

i am trying to use the following code to increment number in a textbox ``` // Animate the element's value from 0 to 1100000: $({someValue: 0}).animate({someValue: 1100000}, { duration: 1000, step: function() { // called on every step // Update the element's text with value: $('#counterx').text(Math.floor(this.someValue+1)); } }); ``` it is working with small numbers like from 0 to 100 but when it comes to large number like in the mentioned code, it is not giving the target number, it is animating to numbers like 1099933 or 1099610 or ..... and every time it changes. so how can i make it to animate to the number i specify?

Original source