Use const wherever possible in js/
[plack-app-gruntmaster.git] / js / 90-timers.js
index 6ec5bdf82a66e416e5f725d5aa3ed55e9d233080..8833a114289a7ee6ef3765f36101f1d766592788 100644 (file)
@@ -1,35 +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;
-                       hours = hours < 10 ? '0' : hours : hours;
-                       var minutes = left / 60 % 60;
-                       minutes = minutes < 10 ? '0' : minutes : minutes;
-                       var seconds = left % 60;
-                       seconds = seconds < 10 ? '0' : seconds : seconds;
-                       timer.innerHTML = 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();
+       }
+});
This page took 0.010965 seconds and 4 git commands to generate.