Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Count up Animation?

I have a a counter that needs to count up from $0 to $10,000 in x seconds (most likely 3 seconds).

Just straight text, kind of like a millisecond countdown timer, but upwards and with a dollar sign.

I'd rather not use a bulky plugin as this just needs to loop through 1-10,00 in x seconds and update every 100ms or so.

I'm stuck at creating the loop that will update, where should I start?


Here is what I've got so far on a click event:

  function countUp() {
    console.log('counted');
    }

    setInterval("countUp()", 1000)
like image 866
wesbos Avatar asked Mar 29 '26 04:03

wesbos


1 Answers

Using jQuery (edited):

$(function(){
    var current = 0;
    var finish = 10000;
    var miliseconds = 3000;
    var rate = 20;

    var counter = setInterval(function(){
         if(current >= finish) clearInterval(counter);
         $("#div").text("$" + current);
         current = parseInt(current) + parseInt(rate);
    }, miliseconds / (finish / rate));
});
like image 128
bschaeffer Avatar answered Apr 02 '26 02:04

bschaeffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!