ponjoh ponjoh - 6 months ago 10
Javascript Question

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 :)

Answer

You should do this:

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

</script>

Assuming 1000 milliseconds.