Restart counter every week

I would like to create a counter that reset every week, I found a code that more or less works, but when It goes to 0, it appears negative…. -1d -1h -2m -5s

            <script>
            // Set the date we're counting down to
            var countDownDate = new Date("Jan 29, 2021 20:21: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) {
                //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
                }
            }
        }, 1000);
      </script>
      <span id="demo"></span>

93 thoughts on “Restart counter every week”

  1. Hello there, just became alert to your blog through Google,
    and found that it’s truly informative. I’m gonna watch out for brussels.
    I will appreciate if you continue this in future.
    Many people will be benefited from your writing.
    Cheers!

    Reply
  2. You are so awesome! I do not think I’ve truly read through anything like that
    before. So wonderful to discover someone with unique thoughts on this subject.
    Seriously.. thanks for starting this up. This web site is something
    that’s needed on the web, someone with a bit of originality!

    Reply
  3. That is really fascinating, You are an excessively skilled blogger.
    I’ve joined your rss feed and look forward to searching for extra of your excellent post.

    Also, I’ve shared your website in my social networks

    Reply
  4. I used to be suggested this blog by means of my cousin. I’m now not certain whether
    or not this put up is written by him as no one else understand such precise approximately
    my trouble. You’re incredible! Thanks!

    Reply
  5. Its like you learn my mind! You seem to grasp so much approximately this,
    like you wrote the book in it or something. I think that you can do with a
    few p.c. to power the message house a bit, however instead of that, that is magnificent
    blog. An excellent read. I’ll definitely be back.

    Reply

Leave a Comment