我试图在页面中的元素内增加一个数字。但我需要包含逗号的数字,而增量只会每秒增加1位数。使用动画增加数量的数量
我的代码工作,但我有点坚持不知道在哪里设置增量仅增加每秒1位(如:10,100,040
然后10,100,041
等)
<script></script>
<div id="el"></div>
// Animate the element's value from x to y:
$({
someValue: 40
}).animate({
someValue: 45000000
}, {
duration: 3000,
easing: 'swing', // can be anything
step: function() { // called on every step
// Update the element's text with rounded-up value:
$('#el').text(commaSeparateNumber(Math.round(this.someValue)));
},
complete: function(){
$('#el').text(commaSeparateNumber(Math.round(this.someValue)));
}
});
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
return "$" + val;
}
罗里嗨,你的代码可以正常使用,可我知道如何设置增加每秒的时间? –
更改'setInterval'中的'1000'参数。这是以毫秒为单位的迭代之间的延迟。你确定要延长它吗?该柜台将在1秒内延期1.5年 –