0

我有一段代码从 0 计数到具有指定延迟的指定数字。

问题是它加 1,我希望它加 0.01

怎么做?代码如下:

    <!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>
4

1 回答 1

3

我还没有测试过这个,但尝试...

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

JS Fiddle玩。

于 2012-11-03T23:09:49.960 回答