Best way to detect midnight and reset data

I am developing a web application toolbar, and I was wondering how to detect that it is midnight, to reset some arrays containing data from the previous day, using jquery or momentjs.

+5
source share
5 answers

Use moment().format("h:mm:ss") , which returns the time in the format h:mm:ss .

 var midnight = "0:00:00"; var now = null; setInterval(function () { now = moment().format("h:mm:ss"); if (now === midnight) { alert("Hi"); } $("#time").text(now); }, 1000); 

JSFIDDLE

It would be best to calculate the seconds before midnight. It is very simple and easy to read using MomentJS:

 // returns the number of seconds until next midnight moment("24:00:00", "hh:mm:ss").diff(moment(), 'seconds'); 

So just do:

 setTimeout( midnightTask, moment("24:00:00", "hh:mm:ss").diff(moment(), 'seconds') ); function midnightTask() { /* do something */ } 

JSFIDDLE

+7
source

Only two ways to achieve this.

  • interrogate every x seconds and see if we are within x seconds of midnight.
  • Calculate the time between the current and midnight and sleeping at this time before performing

(1) has been demonstrated in other answers here (2).

The first thing to do is calculate the number of milliseconds before midnight , then use this as a parameter for javascripts setTimeout .

 setTimeout(function(){ // whatever you want to do at midnight }, msToMidnight); 

After you finish working in this function, you may want to restore the time until the next midnight and repeat the process.

+4
source

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 we haven't checked yet, or if it been more than 30 seconds since the last check if ( !lastUpdate || ( time.getTime() - lastUpdate.getTime() ) > 30000 ) { // Set the last time we checked, and then check if the date has changed. lastUpdate = time if ( time.getDate() !== today.getDate() ) { // If the date has changed, set the date to the new date, and refresh stuff. today = time this_is_where_you_would_reset_stuff() } } } ) 

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 () { // If the date has changed, set the date to the new date, and refresh stuff. today = time this_is_where_you_would_reset_stuff() init() }, midnight.getTime() - today.getTime() ) } init() 

Keep in mind that the second way is likely to be much less reliable.

+3
source

I would try:

 window.setInterval(resetAtMidnight, 1000*5) // every five seconds function resetAtMidnight() { var now = new Date(); if(now.getHours() < 1 && now.getMinutes() < 1 && now.getSeconds() < 5 ) { redrawPage(); } }; 
0
source

Create a date at midnight this morning, add 86400 seconds and set a timeout for this:

 new Date(Date.parse((new Date()).toString().replace(/\d\d:\d\d:\d\d/,'00:00:00')) + 86400 * 1000) 

Here's how you use it:

 var delay_to_midnight = Date.parse((new Date()).toString().replace(/\d\d:\d\d:\d\d/,'00:00:00')) + 86400 * 1000 - Date.now() var timeoutid = window.setTimeout(function() { alert("It midnight!"); }, delay_to_midnight); 
0
source

Source: https://habr.com/ru/post/1204750/


All Articles