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

+3


source to share


8 answers


You must do this:

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

</script>

      



Assuming 1000 milliseconds.

+2


source


Something like that?

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

      



http://jsfiddle.net/iambriansreed/MPP8n/3/

+1


source


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

      

+1


source


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

      

+1


source


0


source


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/

0


source


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
clearInterval(tmrChangeI);

// 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
clearTimeout(tmrChangeI);

      

0


source


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;

 setInterval(function(){ 

    value = value + coef ;

    $this.html(value);

 }, speed);

};

      

And in your main javascript file:

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

      

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

0


source







All Articles