So, I think you are going on this wrong. What you are looking for is not when it is midnight, you just want to know when the day changed, which is much easier.
The first thing I'm going to say is to avoid using timers at all costs. They are not worth it. The performance degradation and extra processor time you get from running the same feature> 3,600 times a day are ridiculous, especially when they run on some other computer. You don't know how much he can handle, so suppose he can't handle it at all. Easily navigate with your users.
I would suggest listening to user input for an event, provided that it is something that you would be on a regular computer, and not something like this , where user input is missing.
If custom input events are something you can rely on, I would do it.
var today = new Date(), lastUpdate; window.addEventListener( "mousemove", function () { var time = new Date();
If you absolutely must use a timer, I would do it.
function init() { var today = new Date(); var midnight = new Date(); midnight.setDate( today.getDate() + 1 ) midnight.setHours( 0 ) midnight.setMinutes( 0 ) setTimeout( function () {
Keep in mind that the second way is likely to be much less reliable.
Kayla source share