+let offset = 0;
+
function update_timer(timer){
var start = parseInt(timer.dataset.start);
var stop = parseInt(timer.dataset.stop);
var value = parseInt(timer.dataset.value);
- var now = Math.floor(Date.now() / 1000);
+ var now = Math.floor(Date.now() / 1000) + offset;
var left = stop - now;
var total = stop - start;
}
}
-$(() => $('.timer').each(item => {
- update_timer(item);
- setInterval(() => update_timer(item), 1000);
-}));
+$(() => {
+ $('.timer').each(item => {
+ update_timer(item);
+ setInterval(() => update_timer(item), 1000);
+ });
+ let xhr = new XMLHttpRequest();
+ xhr.open('HEAD', '/');
+ xhr.onload = () => {
+ let srvtime = Date.parse(xhr.getResponseHeader('Date'));
+ offset = Math.ceil((srvtime - Date.now()) / 1000);
+ console.log("Offset from server: " + offset);
+ };
+ xhr.send();
+});