]>
Commit | Line | Data |
---|---|---|
bbacb12c MG |
1 | let offset = 0; |
2 | ||
7093e9ba MG |
3 | function update_timer(timer){ |
4 | var start = parseInt(timer.dataset.start); | |
5 | var stop = parseInt(timer.dataset.stop); | |
6 | var value = parseInt(timer.dataset.value); | |
bbacb12c | 7 | var now = Math.floor(Date.now() / 1000) + offset; |
20777d84 | 8 | |
7093e9ba MG |
9 | var left = stop - now; |
10 | var total = stop - start; | |
11 | if(left < 0) | |
12 | left = 0; | |
20777d84 | 13 | |
7093e9ba MG |
14 | if(value) { |
15 | value = Math.max(value * 3 / 10, value * left / total); | |
16 | timer.innerHTML = Math.floor(value); | |
17 | } else { | |
b0b9b027 | 18 | var hours = Math.floor(left / 60 / 60); |
7093e9ba | 19 | hours = hours < 10 ? '0' + hours : hours; |
b0b9b027 | 20 | var minutes = Math.floor(left / 60) % 60; |
7093e9ba MG |
21 | minutes = minutes < 10 ? '0' + minutes : minutes; |
22 | var seconds = left % 60; | |
23 | seconds = seconds < 10 ? '0' + seconds : seconds; | |
24 | timer.innerHTML = hours + ':' + minutes + ':' + seconds; | |
20777d84 | 25 | } |
7093e9ba | 26 | } |
20777d84 | 27 | |
bbacb12c MG |
28 | $(() => { |
29 | $('.timer').each(item => { | |
30 | update_timer(item); | |
31 | setInterval(() => update_timer(item), 1000); | |
32 | }); | |
33 | let xhr = new XMLHttpRequest(); | |
34 | xhr.open('HEAD', '/'); | |
35 | xhr.onload = () => { | |
36 | let srvtime = Date.parse(xhr.getResponseHeader('Date')); | |
37 | offset = Math.ceil((srvtime - Date.now()) / 1000); | |
38 | console.log("Offset from server: " + offset); | |
39 | }; | |
40 | xhr.send(); | |
41 | }); |