Clock on webpage using server and system time?

前端 未结 8 956
无人及你
无人及你 2021-01-02 03:34

I need to add a clock to a web page. The clock needs to be synchronized with a server but I really don\'t want to have it constantly check the server as the page will be ope

8条回答
  •  無奈伤痛
    2021-01-02 04:13

    you just get one time date time from server, on load call this method and use moments js for format date:

    var timeMiliSecond = new Date(serverTime).getTime();
     callRepeatedly() {
        setTimeout(() => {
             timeMiliSecond = timeMiliSecond+1000;         
             serverTime=moment(newDate(timeMiliSecond)).format('MM-DD-YYYY HH:mm:ss');
             this.callRepeatedly();
          }, 1000)  
      }
    

提交回复
热议问题