medk
medk

Reputation: 9529

jquery count to number

I have a piece of code that counts from 0 to a specified number with a specified delay.

The problem is that it adds by 1 and I want it to add by 0.01

How to do it? the code is as follows:

    <!DOCTYPE HTML>
<html>
<head>
<style>body{font:11px verdana;color:#555;}</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var max = 20;// set with php
$(incCounter);
function incCounter() {
    var currCount = parseInt($('.counter').html());
    $('.counter').text(currCount+1);
    if (currCount+1 != max) {
        setTimeout(incCounter,50);
    }
}
</script>
</head>
<body>
<div class="counter">0</div>
</body>
</html>

Upvotes: 0

Views: 1661

Answers (1)

slashingweapon
slashingweapon

Reputation: 11307

I haven't tested this, but try...

function incCounter() {
    var currCount = parseFloat($('.counter').html());
    currCount += .01;
    $('.counter').text( currCount.toFixed(2) );
    if (currCount < max)
        setTimeout(incCounter,50);
}

JS Fiddle to play with.

Upvotes: 3

Related Questions