app/docs/.vuepress/public/js/en/get_data.js

73 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-08-01 13:11:05 +08:00
// 定义全局变量来存储数据
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
let globalTotal = 0;
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
let globalOnline = 0;
2024-08-01 12:23:56 +08:00
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
// 从API获取数据并更新全局变量
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
function fetchAndUpdateData() {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
Promise.all([
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
fetch("https://api.liteyuki.icu/count").then(res => res.json()),
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
fetch("https://api.liteyuki.icu/online").then(res => res.json())
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
])
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
.then(([countRes, onlineRes]) => {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
globalTotal = countRes.register;
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
globalOnline = onlineRes.online;
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
})
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
.catch(err => {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
console.error("Error fetching data:", err);
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
});
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
}
2024-08-01 12:28:44 +08:00
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
// 更新页面显示,使用全局变量中的数据
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
function updatePageDisplay() {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
let countInfo = document.getElementById("count-info");
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
if (!countInfo) {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
let info = `<div id="count-info" style="text-align: center; font-size: 20px; font-weight: 500">
2024-08-17 02:24:25 +08:00
Instances:<span id="total">${globalTotal}</span>&nbsp;&nbsp;&nbsp;&nbsp;Online:<span id="online">${globalOnline}</span></div>`;
2024-08-01 13:11:05 +08:00
let mainDescription = document.querySelector("#main-description");
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
if (mainDescription) {
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
mainDescription.insertAdjacentHTML('afterend', info);
2024-08-17 02:24:25 +08:00
2024-08-01 12:28:44 +08:00
}
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
}
2024-08-17 02:24:25 +08:00
2024-08-01 12:28:44 +08:00
}
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
// 初始调用更新数据
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
fetchAndUpdateData();
2024-08-17 02:24:25 +08:00
2024-08-01 13:17:48 +08:00
updatePageDisplay();
2024-08-01 13:11:05 +08:00
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
// 设置定时器,分别以不同频率调用更新数据和更新页面的函数
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
setInterval(fetchAndUpdateData, 10000); // 每10秒更新一次数据
2024-08-17 02:24:25 +08:00
2024-08-01 13:11:05 +08:00
setInterval(updatePageDisplay, 1000); // 每1秒更新一次页面显示