【发布时间】:2020-02-15 08:13:17
【问题描述】:
<div> <h2> Total Steps: <span id="steps" class="commas"></span></h2> </div>
<script>
var from_date = new Date("2020-01-01 00:00:00") /1000;
var now = Date.now() / 1000;
var year2020 = now - from_date;
var speed = 100;
// Random Meter
$(document).ready(function () {
go();
setInterval(function () {
go();
}, speed);
});
var random_increment = Math.floor((Math.random() * 5) + 5);
function go() {
$("#steps").html(year2020.toFixed(0));
year2020 += random_increment;
}
</script>
输出如下:
总步数:3943958(递增)。
现在我想为每 3 位添加逗号,我找到了一个代码
$.fn.digits = function(){
return this.each(function(){
$(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
})
}
那么,我怎样才能得到像
这样的输出总步数:3,943,958(每秒递增)
?
谢谢,
【问题讨论】:
标签: jquery format numbers number-formatting comma