2017-08-26 99 views
0

我試圖獲得以下代碼以在達到指定日期後重置計時器,以便它將在下一週重新啓動。請幫忙!!倒數計時器重置日期

<script> 
    // Set the date we're counting down to 
    var countDownDate = new Date("Aug 26, 2017 0:0:0").getTime(); 

    // Update the count down every 1 second 
    var x = setInterval(function() { 

    // Get todays date and time 
    var now = new Date().getTime(); 

    // Find the distance between now an the count down date 
    var distance = countDownDate - now; 

    // Time calculations for days, hours, minutes and seconds 
    var days = Math.floor(distance/(1000 * 60 * 60 * 24)); 
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
    var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60)); 
    var seconds = Math.floor((distance % (1000 * 60))/1000); 

    // Display the result in the element with id="demo" 
    document.getElementById("demo").innerHTML = days + "d " + hours + "h " 
    + minutes + "m " + seconds + "s "; 

    // If the count down is finished, write some text 
    if (distance < 0) { 
     clearInterval(x); 
     document.getElementById("demo").innerHTML = "GAME DAY"; 
    } 
}, 1000); 
</script> 
+0

日期後到達,如果定時器立即重啓? –

+0

沒有。所以我希望文本能夠在整天內顯示,並且在達到指定日期後的第二天重置。 –

回答

2

更換,如果部分本:

if (distance < 0) { 
    document.getElementById("demo").innerHTML = "GAME DAY"; 
    if(distance < - 1000 * 60 * 60* 24){ // if its past the "game day" 
     // reset timer to next week 
     countDownDate += 1000 * 60 * 60 * 24 * 7 
    } 
} 
+0

即使日期更改爲3天前,計時器仍會顯示遊戲日。 –

+0

@RaznishSmith噢,我沒有注意到你的countDownDate是一個整數。帖子已更新。 –

+0

現在完美運作。非常感謝 –