]>
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 | }); | |
2a1a0906 MG |
33 | if($('.timer').length > 0) { |
34 | let xhr = new XMLHttpRequest(); | |
35 | xhr.open('HEAD', '/'); | |
36 | xhr.onload = () => { | |
37 | let srvtime = Date.parse(xhr.getResponseHeader('Date')); | |
38 | offset = Math.ceil((srvtime - Date.now()) / 1000); | |
39 | console.log("Offset from server: " + offset); | |
40 | }; | |
41 | xhr.send(); | |
42 | } | |
bbacb12c | 43 | }); |