Reputation: 2059
I want to call a jquery function every full hour. So 11:00, 12:00, 13:00 etc.
Here is the function:
function flipIt() {
$('#flip').addClass('animate pulse');
$('#flip').removeClass('animate pulse').delay(1500);
}
How can I call it every full hour?
Upvotes: 2
Views: 2642
Reputation: 3473
Solution which only triggers once per hour:
function alertNextHour() {
var nextAlert = new Date();
nextAlert.setHours(nextAlert.getHours() + 1);
nextAlert.setMinutes(0);
nextAlert.setSeconds(0);
nextAlert.setMilliseconds(0);
var timeToNextAlert = nextAlert - new Date();
setTimeout(function() {
// do what you want
alertNextHour();
}, timeToNextAlert);
}
Upvotes: 1
Reputation: 866
You can do :
setInterval(function(){
if(new Date().getMinutes() === 0) {
flipIt()
}
},60000)
// for testing
setInterval(function(){
if(new Date().getSeconds() === 0) {
alert("new minute !");
}
},1000)
function flipIt() {
$('#flip').addClass('animate pulse');
$('#flip').removeClass('animate pulse').delay(1500);
}
Upvotes: 7
Reputation: 7878
Here is an approach which depends on a dynamic interval. The following function will set the timeout depending on how much time is left to the full hour:
function getDelay(){
var date = new Date();
var hour = date.getMinutes();
return (60 - hour) * 60 * 1000; //get the milliseconds until the next full hour
}
delay = getDelay();
var dynamicInterval = function(){
clearInterval(interval); //stop the current interval
delay = getDelay();
interval = setInterval(dynamicInterval, delay); //set the new interval with the new delay
}
var interval = setInterval(dynamicInterval, delay); //initial start of the interval
Note: the demo deals with seconds instead of minutes.
Upvotes: 1
Reputation: 97
window.setInterval(function(){
/// call your function
}, 3600000);
Upvotes: 0