Raznish Smith
Raznish Smith

Reputation: 67

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>

Upvotes: 0

Views: 763

Answers (1)

Manuel Otto
Manuel Otto

Reputation: 6540

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

Upvotes: 2

Related Questions