2017-04-01 39 views
-1

我是一個新手,javascript和我想創建一個6小時倒計時它執行特定的功能,但時間顯示是完全錯誤的前的Javascript跳過秒

什麼我做錯了,我該怎麼辦讓它在小時,分鐘和秒鐘內正確顯示?

下面是代碼

function startTimer(duration, display) { 
 
    var start = Date.now(), 
 
    diff, 
 
    minutes, 
 
    seconds; 
 

 
    function timer() { 
 
    // get the number of seconds that have elapsed since 
 
    // startTimer() was called 
 
    diff = duration - (((Date.now() - start)/1000) | 0); 
 

 
    // does the same job as parseInt truncates the float 
 
    minutes = (diff/60 * 360) | 0; 
 
    seconds = (diff % 60) | 0; 
 

 
    minutes = minutes < 10 ? "0" + minutes : minutes; 
 
    seconds = seconds < 10 ? "0" + seconds : seconds; 
 

 
    display.textContent = minutes + ":" + seconds; 
 

 
    if (diff <= 0) { 
 
     // add one second so that the count down starts at the full duration 
 
     // example 05:00 not 04:59 
 
     start = Date.now() + 1000; 
 
    } 
 
    }; 
 
    // we don't want to wait a full second before the timer starts 
 
    timer(); 
 
    setInterval(timer, 1000); 
 
} 
 

 
window.onload = function() { 
 
    var fiveMinutes = 60 * 2, 
 
    display = document.querySelector('#time'); 
 
    startTimer(fiveMinutes, display); 
 
};
<span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account

+0

應該'VAR fiveMinutes = 60 * 2'說'VAR fiveMinutes = 60 * 5'? – Carcigenicate

回答

0

<!DOCTYPE html> 
 
<html> 
 

 
<head></head> 
 

 
<body> 
 
    <div id='time'></div> 
 

 
    <script type="text/javascript"> 
 
    function startTimer(duration, display) { 
 

 
     setInterval(function() { 
 
     duration = duration - 1; 
 
     if (duration == -1) { 
 
      clearInterval(duration); 
 
      return; 
 
     } 
 

 
     var seconds = duration % 60; 
 
     var minutes = Math.floor(duration/60); 
 
     var hours = Math.floor(minutes/60); 
 
     minutes %= 60; 
 
     hours %= 60; 
 

 
     display.textContent = hours + ":" + minutes + ":" + seconds; 
 

 
     }, 1000); 
 
    } 
 

 
    window.onload = function() { 
 
     var sixhours = 60 * 60 * 6 
 
     display = document.querySelector('#time'); 
 
     startTimer(sixhours, display); 
 
    }; 
 
    </script> 
 
</body> 
 

 
</html>

+0

它具有@Carcigenicate指定的更正和startTimer函數中的小改動 – user93

+0

我認爲這適用於我,我欣賞 –

0

您計算分鐘每一位執行。僅在秒數達到0時計算分鐘。

function startTimer(duration, display) { 
 
    var start = Date.now(), 
 
    diff, 
 
    minutes, 
 
    seconds; 
 

 
    function timer() { 
 
    // get the number of seconds that have elapsed since 
 
    // startTimer() was called 
 
    diff = duration - (((Date.now() - start)/1000) | 0); 
 
    // does the same job as parseInt truncates the float 
 
    if(!minutes) minutes = (diff/60 * 360) | 0 
 
    seconds = (diff % 60) | 0; 
 
    minutes = seconds<=0?(minutes-1) : minutes; 
 

 
    minutes = minutes < 10 ? "0" + minutes : minutes; 
 
    seconds = seconds < 10 ? "0" + seconds : seconds; 
 

 
    display.textContent = minutes + ":" + seconds; 
 

 
    if (diff <= 0) { 
 
     // add one second so that the count down starts at the full duration 
 
     // example 05:00 not 04:59 
 
     start = Date.now() + 1000; 
 
    } 
 
    }; 
 
    // we don't want to wait a full second before the timer starts 
 
    timer(); 
 
    setInterval(timer, 1000); 
 
} 
 

 
window.onload = function() { 
 
    var fiveMinutes = 60 * 2, 
 
    display = document.querySelector('#time'); 
 
    startTimer(fiveMinutes, display); 
 
};
<span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account

+0

爲什麼此計時器在重新刷新或重新加載頁面時重新開始,實際上我正在嘗試創建會話倒計時計時器我該如何做到這一點 –