I want to make a cooldown that when I reach the end of, for example, 24h, I add 1 day and start the 24h countdown again. If someone could help me, I would be very grateful, thanks in advance. Here's something I've been trying to do:
var countDownYear = new Date().getFullYear();
var countDownMonth = new Date().getMonth() + 1;
var countDownDay = new Date().getDate();
var countDownDate = new Date(countDownMonth + " " + countDownDay + ", " + countDownYear + " 15:27:30").getTime();
var x = setInterval(function () {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.querySelector('#daily-time').innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s";
if (distance < 0) {
console.log(countDownDay);
countDownDay++;
console.log(countDownDay);
clearInterval(x);
}
}, 1000);
<div id="daily-time"></div>
You can add 1 day (in milliseconds to the variable) when the difference is equal to or less than zero and the countdown restarts positive. In the example I put it after filling the div so that the change can be seen in the first second.
If you don't want the jump to be seen, you check it as soon as you calculate the distance variable and recalculate it with the new value.