简体   繁体   中英

Countdown Timer reset date

I'm try to get the following code to reset the timer after the specified date is reached so that it will restart for the following week. Please Help!!

<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>

Replace the if-part with this:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM