I need an equal contact has on these promotion sites, type promotion ends in x days!

Asked

Viewed 54 times

0

I need an equal contact has on these promotion sites, type promotion ends in x days, so I want when it zeroes, example: 00:00:00 it counts 2 days ( or 1 day) forward 48:00:00 and starts the countdown again

<span id="demo"></span>     


// Set the date we're counting down to
                var countDownDate = new Date("Octo 21 , 2019 19:37:25").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);

                    // Output the result in an element with id="demo"
                    document.getElementById("demo").innerHTML =  days +  " : " + hours + " : "
                        + minutes + " : " + seconds + "";



                    // If the count down is over, write some text
                     if (distance < 0) {
                        clearInterval(x);
                        document.getElementById("demo").data = data + 1;
                    }
  • I understand that you are looking for something ready, to just add to your project. I don’t know if you’ve done this yet, but search for funcao Countdown javascript on google. You will find templates ready to adapt your need. In order to make it wait 2 days to re-display the countdown, you must save the Date/Time of the start of the countdown in the cache, cookie, file, etc.

  • So I guess you don’t need to keep the count in the cache, as soon as it zeroes it will add 2 more days, getting 48:00:00, and counting down again

1 answer

2


It is possible to do this as follows:

First the variable countDownDate must be only the object new Date without the property .getTime(), so that it can be amended at a later date:

var countDownDate = new Date("Octo 21 , 2019 11:19:20");

Then you declare another variable by taking the .getTime():

var countDownDateTime = countDownDate.getTime();

And replace the variable in the row below, from countDownDate for countDownDateTime:

var distance = countDownDateTime - now;

In the if that detects when time has run out, you will add 48 hours on the object countDownDate and update the variable countDownDateTime:

if (distance < 0) {
   countDownDate.setHours(countDownDate.getHours()+48);
   countDownDateTime = countDownDate.getTime();
}

Thus, if the date of the object countDownDate for Octo 21 , 2019 11:19:20, when you add 48 hours, you will get Octo 23 , 2019 11:19:20, that is, two days ahead.

The code will look like this:

// Set the date we're counting down to
 var countDownDate = new Date("Octo 21 , 2019 11:27:15");
 var countDownDateTime = countDownDate.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 = countDownDateTime - 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);

     // Output the result in an element with id="demo"
     document.getElementById("demo").innerHTML =  days +  " : " + hours + " : "
         + minutes + " : " + seconds + "";

     // If the count down is over, write some text
      if (distance < 0) {
         countDownDate.setHours(countDownDate.getHours()+48);
         countDownDateTime = countDownDate.getTime();
      }
 });
  • top worked certinho1 Thank you very much

  • Good. Just mark the answer with . Abs!

  • how do I make this marking

  • On the left side of the answer, below the voting arrows.

  • done, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.