亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript倒計時在不同設備上不同步

Javascript倒計時在不同設備上不同步

aluckdog 2022-10-21 17:28:16
我正在為產品發布站點開發倒計時,因此確保同步準確很重要。在我的筆記本電腦上的 netlify 網站上運行它時,它可以工作,并且與在線倒計時相比,它在很大程度上是準確的。但是,當我在手機上打開頁面時,時間完全不同步。整個倒計時javascript如下。 有人能想到解決辦法嗎?//countdownconst countdown = document.querySelector('.countdown');// Set Launch Date (ms)const launchDate = new Date('June 30, 2020 00:00:00').getTime();// Update every secondconst intvl = setInterval(() => {  // Get todays date and time (ms)  const now = new Date().getTime();  // Distance from now and the launch date (ms)  const distance = launchDate - now;  // Time calculation  const days = Math.floor(distance / (1000 * 60 * 60 * 24));  const hours = Math.floor(    (distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)  );  const mins = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));  const seconds = Math.floor((distance % (1000 * 60)) / 1000);  // Display result  countdown.innerHTML = `     <div>${days}<span>Days</span></div>      <div>${hours}<span>Hours</span></div>     <div>${mins}<span>Minutes</span></div>     <div>${seconds}<span>Seconds</span></div>  `;  // If launch date is reached  if (distance < 0) {    // Stop countdown    clearInterval(intvl);    // Style and output text    countdown.style.color = '#17a2b8';    countdown.innerHTML = 'Launched!';    let modal = document.querySelector('.modalDialog');    modal.classList.add('HideModalClass');  }}, 1000);
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

new Date().getTime();將獲取運行它的機器上的時間——如果它在瀏覽器中運行,這將是用戶計算機的本地時間。



查看完整回答
反對 回復 2022-10-21
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號