Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div value increment javascript

I am trying to make a div with an animated value increasing up to it's final value. Here's an example from themeforest: http://demo.themesvision.com/drupal/evolve/ (Happy customer, projects completed, etc)

I've tried lots of different code lines with no success. I was able to do the increment, but can't figure how to make a delay each time there's an increment. I've tried setTimeout() and setInterval().

Here's my code so far:

$(document).ready(function(){
    test();

    function test(){
        var i = 0;
        while ( i <= 100 ) {
            $("#test").text(i);
            i++;
        }
    }
});

Thank in advance!

like image 624
Mind_meddler Avatar asked Dec 13 '25 13:12

Mind_meddler


2 Answers

for (var i = 0; i < 100; i++) { //For loop easier than while loop
    setTimeout(function() { //SetTimeout
        document.getElementById('test').textContent++; //...Where you increment the textContent
    }, i * 20); //...At interval of 20ms
}
like image 124
bjb568 Avatar answered Dec 16 '25 01:12

bjb568


You need something like:

$(document).ready(function(){
    var i = 0;

    test();

    function test(){
        $("#test").text(i++);
        if(i < 100) {
            setTimeout(test, 500);
        }
    }
});
like image 37
iain_simpson Avatar answered Dec 16 '25 01:12

iain_simpson