javascript - Holding on a function for a specific time -
so kind of weird question
i working on project required hold off period of time using javascript, such sending info page example
but, not want line of code executing untill midnight
pseudo code if(midnight){ }
this simplest way explain trying do, know setinterval goes milliseconds , pretty difficult (well me) run every day @ 12am(midnight)
any suggestion & advice great, thank !
function gettimelefttill(targettime){ var time = new date(); var ms = (time.getseconds() + (time.getminutes() + time.gethours() * 60 ) * 60) * 1000 return targettime > ms ? targettime - ms : 24 - (ms - targettime); } function runatmidnight(cb){ var midnightms = 24* 60 * 60 * 1000; //midnight or whenever, e.g midday 12 * 60 * 60 * 1000 var timeleft = gettimelefttill(midnightms); settimeout(function(){ cb(); }, timeleft); } /* * example */ runatmidnight( function(){ alert('it\'s midnight!'); } )
Comments
Post a Comment