js
// script.js// 比赛数据
let matches = [];// 获取比赛数据
const getMatches = () => {const request = newXMLHttpRequest();request.open('GET', 'matches.json');request.onload = () => {if (request.status === 200) {matches = JSON.parse(request.responseText);displayMatches();} else {console.error('Error fetching matches.');}};request.send();
};// 实时更新比赛数据
const updateMatches = () => {const request = new XMLHttpRequest();request.open('GET', 'updates.json');request.onload = () => {if (request.status === 200) {const updates = JSON.parse(request.responseText);matches.forEach((match) => {const update = updates.find((u) => u.id === match.id);if (update) {match.score = update.score;match.progress = update.progress;}});displayMatches();} else {console.error('Error fetching updates.');}};request.send();
};// 每隔 10 秒钟更新一次比赛数据
setInterval(updateMatches, 10000);// 显示比赛数据
const displayMatches = () => {const tbody = document.querySelector('tbody');tbody.innerHTML = '';matches.forEach((match) => {const row = document.createElement('tr');const matchCell = document.createElement('td');const teams = [match.team1, match.team2].join(' vs. ');matchCell.textContent = teams;const scoreCell = document.createElement('td');scoreCell.textContent = match.score;const progressCell = document.createElement('td');progressCell.textContent = match.progress;row.appendChild(matchCell);row.appendChild(scoreCell);row.appendChild(progressCell);tbody.appendChild(row);});
};// 加载初始比赛数据
getMatches();
相关阅读: 实时更新全球所有足球赛事的比分和比赛进程