Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the value of a number in an element every x milliseconds

So I have this simple HTML:

<span id="badge">0</span>

I want the number 0 to increase by 1 every x milliseconds. How do I do that with Javascript (with or without jQuery)?

Thanks a bunch - I'm new to this :)

like image 929
ponjoh Avatar asked Dec 06 '25 08:12

ponjoh


1 Answers

You should do this:

<script>
   var $badge = $('#badge'); // cache 
   setInterval(function () {
        var value = parseInt($badge.html());
        value++;
        $badge.html(value);
   }, 1000);

</script>

Assuming 1000 milliseconds.

like image 184
Richard Avatar answered Dec 07 '25 22:12

Richard