Chrome extension: detecting a new day (day shift) in Javascript?

I know that there is no listener of events on a new day (or an hour / minute, for that matter). But in my Chrome extension, I need to know when a new day started, which means that I will need to use the setInterval function to find out when the day changed. However, I'm not sure what to use for the interval value: 10 seconds or 10 minutes? I am worried about using the processor and memory (constant calls to setInterval), but I still want to know almost as soon as the new day begins. Any ideas on what is the ideal way to handle this?

+3
source share
2 answers

, , - . . 1 5 10

0

, .

function setnewdaytimer(){
    if(window.newdaytimer) clearTimeout(newdaytimer);
    var now= new Date,
    tomorrow= new Date(now.getFullYear(), now.getMonth(), now.getDate()+1); 
    window.newdaytimer= setTimeout(newdayalarm, tomorrow-now);
}
function newdayalarm(){
    alert((new Date()).toLocaleTimeString());
}

window.onload=setnewdaytimer;
+5

All Articles