动态计数器与逗号一起上升

Dynamic Counter Going Up With Commas

本文关键字:一起上 计数器 动态      更新时间:2024-05-11

环顾Stackoverflow,我遇到了这个JSFiddle,但这个脚本内部似乎有错误,由于我的经验,我不完全理解如何修复。

通过JSBeautifier运行,除了以下没有缩进之外,所有代码都缩进得很好;

$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 0, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method for when the element finishes updating
};
})(jQuery);

除此之外,我希望在我的号码中添加逗号,如下所示:

Number(10000).toLocaleString('en');  // "10,000"

1)如何修复一个人在Stackoverflow上回答的代码,无论发现什么错误,都会被评分并标记为答案?

2)如何在输出中添加逗号?

这里有一个插入了addCommas()代码的jsFiddle版本。

它被插入到这行:

$(_this).html(addCommas(value.toFixed(options.decimals)));

如果你想用这个来添加逗号:

Number(10000).toLocaleString('en');  // "10,000"

那么,请确保它在你希望你的应用程序运行的所有浏览器版本中都支持。我不能确定,但它可能需要IE 11。我在IE9和IE10中尝试了你的方法,当没有要求时,它似乎会添加两个小数点。IE11工作正常。

只需在第19行将.replace(/'B(?=('d{3})+(?!'d))/g, ",")添加到value.toFixed(options.decimals):JSFiddle