我正在為產品發布站點開發倒計時,因此確保同步準確很重要。在我的筆記本電腦上的 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);
Javascript倒計時在不同設備上不同步
aluckdog
2022-10-21 17:28:16