// This is a stopper, it will check if string "midnight_passing" in the URL as a query string
// If exist, don't run this code anymore, our job is done. Otherwise, we have work to do.
if ( window.top.location.search.substring(1).indexOf('midnight_passing') == -1 )
{
// PART 1: Define the "precise" time checker and reloader
function midnight_reloader()
{
var d = new Date();
// Some hardcoded values to simulate time, only for testing
var hr = 0;//d.getHours();
var mn = 1;//d.getMinutes();
var sc = 10;//d.getSeconds();
// change this to fit your needs or just leave it
// This will run on 0 hours, on the first minute between 0 and 10 seconds
if ( hr == 0 && mn == 1 && sc < 11 )
{
// If its now 00:01:10, this script will run
// On your URL append "?midnight_passing" to the back of it like here
// we need this to check if the page was reloaded by this script
document.location.href="test.html?midnight_passing";
}
}
// PART 2 : Setup a timer and a loose time checker
// Setup setInterval() callback timer, because we do not need this running
// anytime except near midnight, otherwise its kind of wasteful
var chk_d = new Date();
// simulating time, for testing
var chk_hr = 0;//chk_d.getHours();
var chk_mn = 1;//chk_d.getMinutes();
// If user is here between 11:45PM and 12:02AM, then we need to prepare to reload the page
// this is not precise, we depend on midnight_reloader() instead to check the time further
// Remember that this part runs only on page load, once only.
if ( (chk_hr == 23 && chk_mn > 45) || (chk_hr == 0 && chk_mn < 2) )
{
var midnight_timer = setInterval( midnight_reloader, 10000 );
// But setInterval, will run midnight_reloader() every 10 seconds if all conditions have been met
// So our midnight_reloader() will do the work of checking if its midnight every 10 seconds
}
}