16

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!

mstuercke
  • 731
  • 1
  • 8
  • 19

10 Answers10

25

try:

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

<div id="el"></div>
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • nice i didn't know you could do it this way, i knew about the step function (as you can see in my answer) but this is good stuff! remove the `Math.ceil` and you're golden :) – Ties Sep 07 '12 at 12:10
  • @Ties Math.ceil is used in this case so that its rounded up to upper value, else you'd be getting 9.99858544, etc – Sudhir Bastakoti Sep 07 '12 at 12:13
  • 2
    yeah but you lose the smoothness, you can add a `complete` parameter to finish it – Ties Sep 07 '12 at 12:20
13

What you require is the step parameter in the $().animate function.

var a = 1;
jQuery('#dummy').animate({ /* animate dummy value */},{
    duration: 5000, 
    step: function(now,fx){ 
        a = 1 + ((now/100)*9); 
    }
});

demo

Ties
  • 5,726
  • 3
  • 28
  • 37
11

representation

var snail = {speed:0};
$(snail).animate({speed: 10}, 5000);

demo

2

This should work for you:

var a = 1;
var b = setInterval(function() {
  console.log(a);
  a++;
  if (a == 10) { clearInterval(b); }
}, 500);
Stano
  • 8,749
  • 6
  • 30
  • 44
Eru
  • 675
  • 4
  • 8
  • 2
    jup made a similar [fiddle](http://jsfiddle.net/Fbhs9/2/) note that the timing won't be 100% accurate but that might be not a big problem depending on the case :) – VDP Sep 07 '12 at 12:19
0

Use setInterval :

percentage = 0;
startValue = 1;
finishValue = 5;
currentValue = 1;
interval = setInterval(function(){ 
   percentage ++; 
   currentValue = startValue + ((finishValue - startValue) * percentage) / 100;
   doSomething(currentValue);
   if (percentage == 100) clearInterval(interval);
 }, duration / 100)

function doSomething(val) { /*process value*/}
gabitzish
  • 9,535
  • 7
  • 44
  • 65
0

Html mark up as
Html

<span id="changeNumber">1</span>

You can change its value after 5 seconds.
JQuery:

setInterval(function() {        
        $('#changeNumber').text('10');
    },5000);

Here is a Demo http://jsfiddle.net/Simplybj/Fbhs9/

4b0
  • 21,981
  • 30
  • 95
  • 142
mesimplybj
  • 639
  • 1
  • 5
  • 28
  • in this example you can better use `setTimeout`. You set it to 10 every 5 seconds now... (while it already is 10) – VDP Sep 07 '12 at 12:04
0

As addition to Ties answer - you dont event need to append dummy element to the DOM. I do it like this:

$.fn.animateValueTo = function (value) {

    var that = this;

    $('<span>')
        .css('display', 'none')
        .css('letter-spacing', parseInt(that.text()))
        .animate({ letterSpacing: value }, {
            duration: 1000,
            step: function (i) {
                that.text(parseInt(i));
            }
        });

    return this;
};
ilian6806
  • 221
  • 1
  • 5
0

Try this:

var varToAnimate = 1;
$(window).animate({
    varToAnimate: 10
}, 5000);

Note: This only works if the variable was set with var varToAnimate or window.varToAnimate.

When you set a variable, it creates a property in the window object. jQuery.animate() animates properties, so $(window) gets the window object, and varToAnimate: 10 animates the window's varToAnimate property to 10.

Tiernan Crotty
  • 187
  • 1
  • 10
-1
​var blub = 1;
setTimeout(function () {
    blub = 10;
}, 5000);
kannix
  • 5,107
  • 6
  • 28
  • 47
-1

increment with setTimeout

x = 1

for(i=0;i<1000;i+=100){
  setTimeout(function(){
    console.log(x++)
  },i)
}
Billy Moon
  • 57,113
  • 24
  • 136
  • 237