'Reset js countdown timer and keep it running on page refresh
I want to update this code from here: Reset js countdown timer
to reset every 3 hours, but to also keep running when the user reloads the page - like if the user opens the page at 0:2:57:00 and the user refreshes the page in two minutes the countdown should show 0:2:55:00
Here is the code:
<script>
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 60;
let round = 1;
const diff = 180 ; // set timediff in minutes
//let countDown = new Date('Mar 19, 2022 22:28:00').getTime();
let countDown = new Date(new Date('Apr 1, 2022 18:04').getTime() + diff*60000);
let x = setInterval(function() {
let now = new Date().getTime();
let distance = countDown - now;
if (distance < 0) {
const diff = 180
}
document.getElementById('days').innerText = Math.floor(distance / (day));
document.getElementById('hours').innerText = Math.floor((distance % (day)) / (hour));
document.getElementById('minutes').innerText = Math.floor((distance % (hour)) / (minute));
document.getElementById('seconds').innerText = Math.floor((distance % (minute)) / second);
document.getElementById('dayz').innerText = Math.floor(distance / (day));
document.getElementById('hourz').innerText = Math.floor((distance % (day)) / (hour));
document.getElementById('minutez').innerText = Math.floor((distance % (hour)) / (minute));
document.getElementById('secondz').innerText = Math.floor((distance % (minute)) / second);
}, second)
</script>
I don't know how to fix it. I've searched all the internet!
Thank you very much for any help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
