我正在转换使用非常基本的JSON查询的网站来更新某些统计信息。我和另一个开发人员一起看了这个(我们对JS和JSON都相当缺乏经验)而且让我们有点头疼。
首先,忽略实际数字 - 它们不匹配。考虑一下占位符。
其次,如何解析JSON以获取实时数据并将其设置为data-end
?
最后,这是实现与https://www.compliancemate.com/类似功能的最佳努力(向中间向下滚动页面)
JSON
{"statistic":
{"total-corrective-actions":5171955,
"total-lists":66363,
"total-critical-notifications":167607,
"total-readings":1906320528
}
}
JS
statsLayout: function() {
var d = function(g, f, k, j) {
var h = (g /= j) * g;
var e = h * g;
return f + k * (e * h + -5 * h * h + 10 * e + -10 * h + 5 * g)
};
var a = {
useEasing: d,
useGrouping: "true",
separator: ","
};
var c = $(".number");
$(".layout-stats .number").each(function() {
var e = $(this);
var f = new CountUp(e[0], 0, $(e[0]).data("end") - 100, 0, 5.5, a);
f.start();
setTimeout(function() {
setInterval(function() {
var g = Math.floor(Math.random() * 700) + 100;
setTimeout(function() {
var j = e[0];
var h = $(j).attr("data-end");
++h;
$(j).attr("data-end", h).text(b(h))
}, g)
}, 800)
}, 4000)
});
function b(e) {
var f = Math.round(e);
return f.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}
}
HTML
<div class="statistic">
<span class="number" data-end="1900047125">1,900,047,125</span><br />
<span class="name">Data Collected</span>
</div>
<div class="statistic">
<span class="number" data-end="170409">170,409</span><br />
<span class="name">Critical Calls</span>
</div>
<div class="statistic">
<span class="number" data-end="5151905">5,151,905</span><br />
<span class="name">Corrective Actions</span>
</div>