-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop-script.js
54 lines (50 loc) · 1.58 KB
/
top-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var lastResponse = "";
var timeList = document.getElementById("time-list");
var pointList = document.getElementById("point-list");
update();
setInterval(update, 60000);
function update()
{
get("https://gnome.firebaseio.com/top.json", function (body)
{
if (body !== lastResponse)
{
lastResponse = body;
timeList.innerHTML = "";
pointList.innerHTML = "";
var i, tr;
var top = JSON.parse(body);
for (i = 0; i < top.time.length; i++)
{
tr = document.createElement("tr");
tr.innerHTML = "<td>" + (i + 1) + "</td><td>" + top.time[i].user + "</td><td>" + formatTime(top.time[i].time) + "</td>";
timeList.appendChild(tr);
}
for (i = 0; i < top.time.length; i++)
{
tr = document.createElement("tr");
tr.innerHTML = "<td>" + (i + 1) + "</td><td>" + top.points[i].user + "</td><td>" + top.points[i].points + "</td>";
pointList.appendChild(tr);
}
}
})
}
function get(url, cb)
{
var req = new XMLHttpRequest();
req.onreadystatechange = function ()
{
if (req.readyState === 4 && req.status === 200)
{
cb(req.responseText);
}
};
req.open("GET", url);
req.send();
}
function formatTime(time)
{
if (time < 60) return time + "m";
if (time < 1440) return Math.floor(time / 60) + "h " + time % 60 + "m";
return Math.floor(time / 1440) + "d " + Math.floor((time % 1440) / 60) + "h " + time % 60 + "m";
}