Change JavaScript interval time when current time is between two times -
i’m looking way tweak current script of mine loads page div every minute. want wait 5 minutes @ specific time, go executing every minute. here’s have far.
var starttime = 10:30:00 pm; var endtime = 10:35:00 pm; var myvar = setinterval(function(){ mytimer() }, 1000); function mytimer() { var d = new date(); document.getelementbyid("currenttime").innerhtml = d.tolocaletimestring(); if ( d.tolocaletimestring() > starttime && d.tolocaletimestring() < endtime ) { setinterval(function() { }, 300000); $("#ticketload").load("loadlasttenminutesmodified.php"); } else { setinterval(function() { }, 60000); $("#ticketload").load("loadlasttenminutesmodified.php"); } };
any appreciated, thanks.
var starttime = '10:30:00 pm', endtime = '10:35:00 pm'; var myvar = setinterval(mytimer, 1000); // use function instead of tolocaletimestring, // since inconsistencies may arise one, depending on country. function gettimeformatted(date) { var hours = date.gethours(), minutes = date.getminutes(), seconds = date.getseconds(), ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; minutes = minutes < 10 ? '0' + minutes : minutes; seconds = seconds < 10 ? '0' + seconds : seconds; return hours + ':' + minutes + ':' + seconds + ' ' + ampm; } function mytimer() { var d = gettimeformatted(new date()); document.getelementbyid("currenttime").innerhtml = d; // return (exit) function when current time // between both of dates. // function not go further this. // , nothing other update innerhtml // 5 minutes. if (d > starttime && d < endtime) return; // stuff supposed run every minute. // assume is, place whatever else want in here. $("#ticketload").load("loadlasttenminutesmodified.php"); }
you can exit out of interval return
when time criteria met, before executing rest of code supposed run every minute.
i made changes code:
- you missed quotes in
starttime
,endtime
variables - replaced
tolocaletimestring
new function. @spiderpig pointing out whytolocaletimestring
isn't reliable. - you can specify function directly in
myvar
interval, instead of executingmytimer
function inside of anonymous function. - format current day am/pm once, since that's needed.
return
when time criteria met , before executing rest of code inside of interval.- i don't know empty
setinterval
s in there for, removed them. guess example code, judging variable names gave.
Comments
Post a Comment