Reputation: 1277
for example i want to call a js function at 10.00.00.00 am how can i do?
<script type="text/javascript">
var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 30, 0, 0) - now;
setTimeout(function{openAPage(), setInterval(openAPage, 60*1000)}, millisTill10)
function openAPage() {
var startTime = new Date().getTime();
var myWin = window.open("http://google.com","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;
document.write("<br>button pressed@</br>")
document.write(new Date(startTime));
document.write("<br>page loaded@</br>")
document.write(new Date(endTime));
document.write("<br>time taken</br>")
document.write(timeTaken);
myWin.close()
}
</script>
i expect from this code at 00.30 it will open google and then every 1 minute later it will do it again? whats wrong with that code?
Upvotes: 102
Views: 133375
Reputation: 576
Here is a simplified version of @VirtualVAT's method, which is superior to calculating a timeout delay (as in all other answers) due to the potential for interruptions. Note this simpler version will call the desired function immediately if the time has passed (as opposed to delaying until tomorrow).
var runAt = new Date(new Date().toDateString()+' 10:00')
, runFn = ()=>alert("It's 10am!")
, timer = ()=>{
if (new Date() - runAt >= 0) runFn();
else setTimeout(timer,1000);
};
timer();
Upvotes: 0
Reputation: 71
The best algorithm here is great but there could be a problem if computer slept some time during this period especially if there is a long time to "time". In this case waiting time period will be longer for sleeping time since "timeout" is put on hold while sleeping. Also setTimeout() is not so accurate to rely on it for so long period. If there is a need to watch for some long period I'd suggest modified algorithm above to check for the specified time each e.g. 1 second:
const start = new Date();
var millisTill10 = new Date(start.getFullYear(), start.getMonth(), start.getDate(), 10, 0, 0, 0) - start;
if (millisTill10 < 0)
millisTill10 += 86_400_000; // it's after 10am, try 10am tomorrow.
function timeWatcher() {
if (new Date() - start >= millisTill10)
alert("It's 10am!");
else
setTimeout(timeWatcher, 1000);
}
timeWatcher();
Upvotes: 1
Reputation: 1780
Try this
$(document).ready(function() {
setTimeToTrigger();
});
function setTimeToTrigger(){
var dt = new Date();
var hour = dt.getHours();
var minute = dt.getMinutes() ;
var seconds = dt.getSeconds();
if(hour<10){
nexthour=9;
}else if(hour<22){
nexthour=21;
}else{
nexthour=23-hour+9;
}
delaytime=(nexthour-hour)*60*60+(59-minute)*60+(59-seconds);
alert('will be triggered in :'+ delaytime + ' seconds');
setTimeout( function() {
alert("The time is 10:00");
}, delaytime*1000);
}
<html>
<head>
<title>alert at specific time</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h3>Alert at at 10:00 AM and 10:PM</h3>
</body>
</html>
Upvotes: 0
Reputation: 1270
My solution for running a script at a specific time, btw no error checking for negative timeout.
//year, month 0-11, date, hour, min (can add ,sec,msec)
var eta_ms = new Date(2015, 0, 21, 17, 0).getTime() - Date.now();
var timeout = setTimeout(function(){}, eta_ms);
Upvotes: 23
Reputation: 90742
You'll need setTimeout to set a timer and Date to calculate how long the timer needs to go until it triggers.
var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
if (millisTill10 < 0) {
millisTill10 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function(){alert("It's 10am!")}, millisTill10);
Upvotes: 172
Reputation: 71565
Assuming the code is located on a web page that will be loaded before 10:00 and will still be viewed at 10:00, you can use setTimeout()
to set up a timed event. the function takes some JS statement to execute, and the number of milliseconds before it should execute. You can find this second part pretty easily with the built-in date functions.
Upvotes: 0