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