如何在使用setInterval()更新的值中添加逗号
How to Add commas to Values being updated with setInterval()
我有一个脚本,可以计算每秒有多少只小狗和小猫。它使用Date()来计算一周初和一个月初有多少人出生。但我很难更新这个脚本来添加成千上万、数百万、数十亿的逗号。有人能告诉我在这个脚本中添加逗号的最佳方法吗?
jsFiddle 来了
var start = new Date(),
midnight = new Date(start.getFullYear(), start.getMonth(), start.getDate(), 0),
first = new Date(start.getFullYear(), start.getMonth(), 1);
var now = new Date(),
secondsFromStart = Math.floor((now - start)/1000),
secondsFromMidnight = Math.floor((now - midnight)/1000),
secondsFromFirst = Math.floor((now - first)/1000);
var elems = [];
$(".s").each(function(){
var $this = $(this);
var BornPerSec = $this.data("quantity"),
Start = secondsFromStart*BornPerSec,
Midnight = secondsFromMidnight*BornPerSec,
First = secondsFromFirst*BornPerSec;
elems.push({
obj: $this,
BornPerSec: BornPerSec,
Start : Start,
Midnight : Midnight,
First : First,
now: $this.children('.now'),
morning: $this.children('.morning'),
month: $this.children('.month'),
});
});
setInterval(function () {
$.each(elems,function(i,n){
n.Start+=n.BornPerSec;
n.Midnight+=n.BornPerSec;
n.First+=n.BornPerSec;
n.now.text(n.Start % 1 === 0 ?n.Start: n.Start.toFixed(2));
n.morning.text(n.Midnight % 1 === 0 ?n.Midnight:n.Midnight.toFixed(2));
n.month.text(n.First % 1 === 0 ?n.First:n.First.toFixed(2));
});
}, 1000);
您可以尝试使用此函数:
function numberWithCommas( x ) {
return x.toString().replace( /'B(?=('d{3})+(?!'d))/g, ',');
}
更新jsFiddle演示
相关文章:
- 添加文字和评论功能更新Div
- 通过jQuery添加ng样式属性,angular不更新
- 更新数据,而不是用javascript和jquery将其添加到我的表中
- JSON:loop Invoice Items:如果InvoiceNo未退出则添加,如果退出则更新值,示例包括在内
- 如何使用jQuery来添加分段的表单字段,并在添加不同值时进行更新
- 如何在猫鼬中更新文档后添加一些操作
- 如何在jQuery中的单个保存按钮上更新数据和添加数据
- 通过Couchdb中的更新处理程序添加id和author字段
- 如何在使用setInterval()更新的值中添加逗号
- 如何更新浏览器'使用Ajax将html添加到页面后的缓存
- 向HTML页面添加按钮,更新表格信息
- 在媒体窗口中选择图片后添加/更新自定义字段(WordPress)
- 异步添加/更新保存方法未更新
- 连续添加|更新|将数据库中的数据刷新到页面而不回发
- jQuery-ajax在每个/任何ajax请求的数据中添加更新的变量
- 在map中嵌套的列表中添加/更新一个值
- geoJson属性添加/更新
- Angular从用户输入的属性和值中添加/更新json对象
- 使用jquery在URL中添加/更新参数
- D3.js没有在HTML中添加更新的树