67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
|
document.addEventListener('DOMContentLoaded', () => {
|
||
|
new ResultService();
|
||
|
});
|
||
|
|
||
|
class ResultService
|
||
|
{
|
||
|
hash = 0;
|
||
|
alertbox = document.getElementById("alert");
|
||
|
table = document.getElementById("ranking-table");
|
||
|
endpoint = "../api/v1/race/sync.php?h=";
|
||
|
|
||
|
constructor()
|
||
|
{
|
||
|
this.loop();
|
||
|
}
|
||
|
|
||
|
async loop()
|
||
|
{
|
||
|
try {
|
||
|
await this.updateTable();
|
||
|
this.alertbox.classList.add("hidden");
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
this.alertbox.innerHTML = "<b>Noe gikk galt: </b>" + error;
|
||
|
this.alertbox.classList.remove("hidden");
|
||
|
}
|
||
|
|
||
|
setTimeout(() => {
|
||
|
this.loop();
|
||
|
}, 1000);
|
||
|
}
|
||
|
|
||
|
async updateTable()
|
||
|
{
|
||
|
let response = await fetch(this.endpoint + this.hash);
|
||
|
|
||
|
if (response.status === 204)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let json = await response.json();
|
||
|
|
||
|
this.hash = json.hash;
|
||
|
|
||
|
let data = json.data;
|
||
|
|
||
|
let tableHTML = "";
|
||
|
tableHTML += "<table>";
|
||
|
tableHTML += "<tr>";
|
||
|
tableHTML += "<th>Lag</th>";
|
||
|
tableHTML += "<th>Tid</th>";
|
||
|
tableHTML += "</tr>";
|
||
|
|
||
|
data.times.reverse().forEach(element => {
|
||
|
tableHTML += "<tr>";
|
||
|
tableHTML += "<td>" + data.map.team.name[element.id] + "</td>";
|
||
|
tableHTML += "<td>" + element.time + "</td>";
|
||
|
tableHTML += "</tr>";
|
||
|
});
|
||
|
|
||
|
tableHTML += "<table>";
|
||
|
|
||
|
this.table.innerHTML = tableHTML;
|
||
|
}
|
||
|
}
|