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