0 I want t...">

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

So, I have this simple HTML:

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


I want the number 0 to increment by 1 every x milliseconds. How can I do this using Javascript (with or without jQuery)?

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


source to share

8 answers

You must do this:

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



Assuming 1000 milliseconds.



Something like that?

var millisecs = 10;
setInterval(function() {
  var $badge = $('#badge');
}, millisecs);





function increment() {
    document.getElementById("badge").value = Number(document.getElementById("badge").value) + 1;




Each of the answers I see here has the same disadvantages:

  • due to DOM element selection every ms. Especially when using a large library like jQuery.
  • setInterval()

    is probably a tool designed for this functionality, but not reliable. This can be very different from real time, especially when using a small interval. If you want to exactly execute x per second, you can google for some time libraries.

I would code:

var textNode = document.getElementById(badge).firstChild;
var start = Date.now();
window.setInterval(function update() {
    textNode.data = Math.round((new Date()-start)/ms);
}, ms);


If you don't want to start at 0, it would be trivial to add an offset (determined before the start of the loop), for example.

var start = Date.now() - (textNode.data * ms || 0); // NaN catching, implicit number cast






You can use setInterval


var $badge = $('#badge');
setInterval(function () {
     $badge.html(parseInt($badge.html()) + 1);
}, 1);​//Specify the milliseconds here, right it will update the value every 1 millisecond


Working demo - http://jsfiddle.net/8FMZh/



A little more about timers :

// setting a variable for your timer will allow you the ability to "turn it on and off"
var tmrChangeI;
// setTimeout is a function to initiate a function once after given amount of milisecs
// whereas setInterval will continue a function until cancled every so many milisecs

// the following wil "turn on" your timer
tmrChangeI = setInterval(function() {
    var $badge = $('#badge');
    $badge.html($badge.html() + 1);
}, 500); // 500 will = every half of a second

// to "turn off" timer

// or set a bool to test and use timeout to repeat till bool is false
var tmrBool = true;

// establish function to execute
function tmrFunc() {
    var $badge = $('#badge');
    $badge.html($badge.html() + 1);
    if (tmrBool) tmrChangeI = setTimeout(function() { tmrFunc(); }, 500); // 500 will = every half of a second

// execute function, begin timer
tmrChangeI = setTimeout(function() { tmrFunc(); }, 500);

// clear via bool allowing one more execution
tmrBool = false;

// clear by force possibly stoping next execution,
// tho in this manner it may be too late if timer is very short 
// and maybe overriden by bool still being true, this is not safest 
// but is example of how to use setTimeout




You can create a Jquery plugin so you can reuse it whenever you need.

$.fn.increment= function(options) {

 var $this = $(this);

 var coef = options.coef;

 var speed = options.speed;

 var value = 0;


    value = value + coef ;


 }, speed);



And in your main javascript file:

$("#badge").increment({coef: 1, speed:1000});


working demo: http://jsfiddle.net/8FMZh/102/



All Articles