Call jQuery Ajax Request Each X Minutes
How can I call an Ajax Request in a specific time Period? Should I use Timer Plugin or does jQuery have a plugin for this?
You can use the built-in javascript setInterval.
var ajax_call = function() {
//your jQuery ajax code
};
var interval = 1000 * 60 * X; // where X is your every X minutes
setInterval(ajax_call, interval);
or if you are the more terse type ...
setInterval(function() {
//your jQuery ajax code
}, 1000 * 60 * X); // where X is your every X minutes
A bit late but I used jQuery ajax method. But I did not want to send a request every second if I haven't got the response back from the last request, so I did this.
function request(){
if(response == true){
// This makes it unable to send a new request
// unless you get response from last request
response = false;
var req = $.ajax({
type:"post",
url:"request-handler.php",
data:{data:"Hello World"}
});
req.done(function(){
console.log("Request successful!");
// This makes it able to send new request on the next interval
response = true;
});
}
setTimeout(request(),1000);
}
request();