Reputation: 15
I'm trying to write some javascript that constantly checks for the current second of the time and if it matches a certain second value to refresh the page and continue doing that, so my page will always refresh at specified second intervals based on the current time. For example: 3:01:10 PM, 3:01:15 PM, 3:01:20 PM, 3:02:10 PM, etc.
My code below doesn't seem to be working - any ideas how I could accomplish this? Thanks!
var seconds = new Date().getSeconds();
setTimeout(function(){
if (seconds == 10 || seconds == 15 || seconds == 20) {
window.location.reload(1);
}
}, 1000);
Upvotes: 1
Views: 205
Reputation: 795
Your issue is that you never update the value of "seconds". Try moving it into the function.
var seconds;
setTimeout(function(){
seconds = new Date().getSeconds();
if (seconds == 10 || seconds == 15 || seconds == 20) {
window.location.reload(1);
}
}, 1000);
Upvotes: 0
Reputation: 207861
You get the value of second
exactly one time with your code. You need to move the variable within the function and change setTimeout
to setInterval
:
setInterval(function(){
var seconds = new Date().getSeconds();
if (seconds == 10 || seconds == 15 || seconds == 20) {
window.location.reload(1);
}
}, 1000);
Upvotes: 3