X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=js%2F90-timers.js;h=8833a114289a7ee6ef3765f36101f1d766592788;hb=65d5cb3b6007fa0bc758722983b3f0afefc02f2e;hp=2d6cbfb144bdb8e4d9dcb306d45374073394185d;hpb=20777d844ac7adb5887ea4e50885cd9702b7ee6a;p=plack-app-gruntmaster.git diff --git a/js/90-timers.js b/js/90-timers.js index 2d6cbfb..8833a11 100644 --- a/js/90-timers.js +++ b/js/90-timers.js @@ -1,32 +1,40 @@ -(function(){ - 'use strict'; +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); +function update_timer(timer){ + const start = parseInt(timer.dataset.start); + const stop = parseInt(timer.dataset.stop); + const value = parseInt(timer.dataset.value); + const now = Math.floor(Date.now() / 1000) + offset; - var left = stop - now; - var total = stop - start; - if(left < 0) - left = 0; + const left = Math.max(stop - now, 0); + const total = stop - start; - if(value) { - value = Math.max(value * 3 / 10, value * left / total); - timer.innerHTML = Math.floor(value); - } else { - var hours = left / 60 / 60; - var minutes = left / 60 % 60; - var seconds = left % 60; - timer.innerHTML = sprintf('%02d:%02d:%02d', hours, minutes, seconds); - } + if(value) + timer.innerHTML = Math.floor(Math.max(value * 3 / 10, value * left / total)); + else { + let hours = Math.floor(left / 60 / 60); + hours = hours < 10 ? '0' + hours : hours; + let minutes = Math.floor(left / 60) % 60; + minutes = minutes < 10 ? '0' + minutes : minutes; + let seconds = left % 60; + seconds = seconds < 10 ? '0' + seconds : seconds; + timer.innerHTML = hours + ':' + minutes + ':' + seconds; } +} - $( document ).ready(function() { - $('.timer').each(function(index, item){ - update_timer(item); - setInterval(function(){update_timer(item)}, 1000); - }); +$(() => { + $('.timer').each(item => { + update_timer(item); + setInterval(() => update_timer(item), 1000); }); -})(); + if($('.timer').length > 0) { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', '/'); + xhr.onload = () => { + const srvtime = Date.parse(xhr.getResponseHeader('Date')); + offset = Math.ceil((srvtime - Date.now()) / 1000); + console.log("Offset from server: " + offset); + }; + xhr.send(); + } +});