Guest User

Javascript server time

a guest
Nov 15th, 2019
242
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 = agoraValue + 1000;
  23.   };
  24.    
  25.   // Await DOM to be loaded
  26.   if (document.readyState === 'loading') {
  27.     document.addEventListener('DOMContentLoaded', () => {
  28.       doDom();
  29.     });
  30.   } else {
  31.     doDom();
  32.   }
Advertisement
Add Comment
Please, Sign In to add comment