【发布时间】:2017-04-30 06:24:07
【问题描述】:
我有一个有开始日期但没有结束日期的计数器。基本上,在开始日期之后经过的每一秒都会增加 41.70 美元/秒。这种情况会无限期地持续下去。
我已经构建了功能。我唯一苦苦挣扎的部分是我希望它像从美分开始的自动收报机一样计数。 (例如 1,200.98 -> 1,200.99 -> 1,201.00 等)
以下是我想要调整的 Count Up 功能示例。虽然我不会像他们在示例中那样使用“数据计数”属性。
$('.counter').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({ countNum: $this.text()}).animate({
countNum: countTo
},
{
duration: 8000,
easing:'linear',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
//alert('finished');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="counter" data-count="2200">0</div>
这是我的代码:
window.onload = function() {
startDate("May 01, 2017 00:00:00 EST");
};
function startDate(x) {
rightNow = new Date();
x = new Date(x);
difference = rightNow - x;
amtPerSecond = (41.70 * (difference / 1000)).formatMoney(2);
$("#perSecond").text("$ " + amtPerSecond);
clearTimeout(startDate.to);
startDate.to = setTimeout(function() {
startDate(x);
}, 1000);
}
// Plug-in to combact safari's incompatibility with .toLocaleString()
Number.prototype.formatMoney = function(c, d, t) {
var n = this,
c = isNaN((c = Math.abs(c))) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = String(parseInt((n = Math.abs(Number(n) || 0).toFixed(c)))),
j = (j = i.length) > 3 ? j % 3 : 0;
return (
s +
(j ? i.substr(0, j) + t : "") +
i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
(c ? d + Math.abs(n - i).toFixed(c).slice(2) : "")
);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="perSecond"></div>
如有任何帮助,我们将不胜感激。
【问题讨论】:
标签: javascript jquery counter