Guest User

Server Time Javascript

a guest
Nov 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   /*
  2.   <table>
  3.     <td id="relogio" data-server-time="<?php echo date('Y,m,d,H,i,s'); ?>"></td>
  4.   </table>
  5.   */
  6.  
  7.   let agoraValue = 0;
  8.  
  9.   const doDom = () => {
  10.     const relogio = document.querySelector("#relogio");
  11.     const b = relogio.getAttribute("data-server-time").split(",");
  12.     const agora = new Date(b[0], (b[1]-1), b[2], b[3], b[4], b[5]);
  13.     agoraValue = (agora.valueOf());
  14.     setInterval(iniciaCronometro,1000);
  15.   };
  16.      
  17.   const iniciaCronometro = () => {
  18.     const myDate = new Date(agoraValue);
  19.     const h = myDate.getHours();
  20.     const i = ("0" + String(myDate.getMinutes())).slice(-2);
  21.     relogio.innerHTML = h+':'+i;
  22.     agoraValue = (myDate.valueOf()+1000);
  23.     agoraValue = (timeAgora + 1);
  24.   }
  25.    
  26.   // Await DOM to be loaded
  27.   if (document.readyState === 'loading') {
  28.     document.addEventListener('DOMContentLoaded', () => {
  29.       doDom();
  30.     });
  31.   } else {
  32.     doDom();
  33.   }
Advertisement
Add Comment
Please, Sign In to add comment