Reputation: 58632
I have a dashboard page that users left open on the TV overnight.
I noticed there was a bug last night, it needs to reload
as soon as it detects a new day.
Let's say today is 4/29
. So, right at the first second of 4/30
, I need to reload the page.
I use moment.js in my application. I'm not sure if I should load the page at midnight 12:00 PM
or at 12:01 AM
. I'm afraid that when I do it at 12:00 PM, it still considering the day as 4/29
making the reloading never happen and the bug 🐛 still being there.
if(moment("24:00:00", "hh:mm:ss").diff(moment(), 'seconds') == 0){
location.reload();
}
or should I do this?
if(moment().format("h") == 12){
location.reload();
}
How do I make sure that? How to verify that ?
I have this code
//====================================
// RUN every 1 mn = 60 s
//====================================
window.setInterval(function () {
getNextFeed('{{ $baby->id }}');
if(moment("24:00:00", "hh:mm:ss").diff(moment(), 'seconds') == -1){
location.reload();
}
}, 60000);
I just added it there.
Upvotes: 3
Views: 869
Reputation: 6532
No need for moment at all, here is vanilla solution that will reload page if time is 23:59, and will check if that is true every second.
You can lift an interval to few more seconds or every half minute, it will make no change and set your desired time inside if (nowHour == 23 && nowMinuts == 59)
If you need to target seconds also, then include that in if
and leave interval at every second.
But if you include second cheeking then do : if (nowHour == 23 && nowMinuts == 59 && nowSecunds >= 55)
with >=
to make sure it will fire with 5 second window just in case.
But i believe you are perfectly fine with just minutes.
setInterval(function() {
let nowHour = new Date().getHours()
let nowMinuts = new Date().getMinutes()
let nowSecunds = new Date().getSeconds()
console.log("Hour " + nowHour + " Minutes " + nowMinuts + " Secunds " + nowSecunds)
if (nowHour == 23 && nowMinuts == 59) {
location.reload();
}
}, 1000);
Upvotes: 3
Reputation: 502
You may store the current date when page loaded, and if the date changed, it means "next day" started.
const getDate = () => moment().format('YYYY-MM-DD')
const pageLoadDate = getDate() // store current date, e.g. 2021-04-29
setInterval(() => {
if (pageLoadDate !== getDate()) {
location.reload()
}
}, 60000)
Upvotes: 2