jquery "animate" variable value

mstuercke picture mstuercke · Sep 7, 2012 · Viewed 18k times · Source

I need to "animate" a variable with jquery.

Example: The variable value is 1. The value should be 10 after 5 seconds. It should be increase "smoothly".

Hope that you know what I mean.

Thank you!

Answer

Sudhir Bastakoti picture Sudhir Bastakoti · Sep 7, 2012

try:

$({someValue: 0}).animate({someValue: 10}, {
    duration: 5000,
    step: function() { 
        $('#el').text(Math.ceil(this.someValue));
    }
});

<div id="el"></div>