Reputation: 23293
I know there's not a event listener for a new day (or hour/minute, for that matter). But in my Chrome Extension, I need to know when a new day has started, and which means I'll have to use a setInterval function to find out when the day has changed. However, I'm not sure what to use for the interval value: 10 seconds, or 10 minutes? I'm worried about using up CPU & memory (constant setInterval calls), but I still want to know almost as soon as a new day starts. Any ideas as to what is the ideal way to handle this?
Upvotes: 3
Views: 2077
Reputation: 104790
If you mean a new day for the user, you can set a single timeout when the page loads.
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;
Upvotes: 5
Reputation: 40179
Depending how exact you want to be, the only way doing this is by setting an interval. You can set your precision to be whatever you want. 1min 5min 10min
Upvotes: 0