How can I call an Ajax Request in a specific period of time? Should I use Timer Plugin or does jQuery have a plugin for this?
-
2You can google this. http://stackoverflow.com/questions/4542863/jquery-ajax-call-with-timer – Zabba Feb 08 '11 at 07:06
8 Answers
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

- 9,725
- 6
- 23
- 28
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();
-
Why not just add `setTimeout(request(),1000);` inside the req.done and avoid the need to check the bool? – Wobbles Oct 09 '15 at 16:50
-
Well, good thinking. But the bool can be great for checking if the request has come forward anywhere else in the code. We say you can only click a submit button when the response has come back, then you can use that bool to check whether the response has come or not. – linslusa Nov 03 '16 at 13:53
you can use setInterval()
in javascript
<script>
//Call the yourAjaxCall() function every 1000 millisecond
setInterval("yourAjaxCall()",1000);
function yourAjaxCall(){...}
</script>

- 3,810
- 7
- 32
- 51

- 61
- 1
No plugin required. You can use only jquery.
If you want to set something on a timer, you can use JavaScript's setTimeout
or setInterval
methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );

- 5,882
- 3
- 33
- 44

- 27,666
- 26
- 83
- 129
You have a couple options, you could setTimeout()
or setInterval()
. Here's a great article that elaborates on how to use them.
The magic is that they're built in to JavaScript, you can use them with any library.

- 7,734
- 6
- 33
- 34
use jquery Every time Plugin .using this you can do ajax call for "X" time period
$("#select").everyTime(1000,function(i) {
//ajax call
}
you can also use setInterval

- 16,587
- 26
- 100
- 160
-
link is broken and what is the advantage of it, can't we just use setinterval instead? – Suneel Kumar Sep 11 '17 at 06:05
I found a very good jquery plugin that can ease your life with this type of operation. You can checkout https://github.com/ocombe/jQuery-keepAlive.
$.fn.keepAlive({url: 'your-route/filename', timer: 'time'}, function(response) {
console.log(response);
});//

- 419
- 6
- 13
Instead of having an absolute repeating timer, you should call the function after completing the initial request (like a recursive function).
This ensures that requests will be sent only after completing the previous one. This avoids the issues like queuing up of requests and thereby Denial of Service.
(function ajaxRequest() {
$.ajax('url_of_your_application.php', {
type: 'post',
data: {
phone: '1234567890',
},
})
.done(function (data) {
// Do whatever you want with the data.
})
.always(function (data) {
// We are starting a new timer only AFTER COMPLETING the previous request.
setTimeout(ajaxRequest, 5000);
});
})();

- 1,099
- 10
- 13