如何从头开始在线显示计数器



我的网站上有一个脚本,它在不刷新页面的情况下显示(GTA中服务器玩家的在线计数器(。计数器脚本

var updateInterval = 700;
setInterval(updatePlayerCount, updateInterval);
// Function to run update logic
function updatePlayerCount() {
var ip = "rage2.grand-rp.su:22005";
$.getJSON('https://cdn.rage.mp/master', function(masterlist) {
$.each(masterlist, function(key, result) {
if(key == ip) {
document.getElementById('online').innerHTML = result.players;
return false;              
}
});
});
}

它的工作原理应该是,当页面加载时,计数器从零开始,一直到现在联机的数字。

我发现了这样一个代码,页面加载时计数器从零开始,一直到html中的数字,但它与上面的代码是分开工作的。如何让他们一起工作?html 中从零到数字的起始计数器

$('#online').each(function () {
$(this).prop('Counter',0).animate({
Counter:$(this).text()
},{
duration: 4000,
easing: 'swing',
step: function(now){
$(this).text(Math.ceil(now));
}
});

});

您可以将计数器代码添加到函数中,然后将该函数添加到getJSON回调中。此外,如果你正在寻找一个记录,你不需要每个,你只需要看看这个IP是否作为密钥存在。

var updateInterval = 700;
_timer = setInterval(updatePlayerCount, updateInterval);
function ShowCounter() {
clearInterval(_timer);
$('#online').each(function() {
$(this).prop('Counter', 0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now));
}
});
});
}
function updatePlayerCount() {
var ip = "rage2.grand-rp.su:22005";
$.getJSON('https://cdn.rage.mp/master', function(masterlist) {
if(masterlist[ip] != undefined){
document.getElementById('online').innerHTML = masterlist[ip].players;
ShowCounter();
}
});
}

最新更新