调用jQuery Ajax请求每个X分钟

如何在特定的时间段内调用Ajax请求? 我应该使用定时器插件还是jQuery有这个插件?

您可以使用内置的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); 

或者如果你是更简洁的types…

 setInterval(function() { //your jQuery ajax code }, 1000 * 60 * X); // where X is your every X minutes 

你有几个选项,你可以setTimeout()setInterval() 。 这是一篇很好的文章,详细说明如何使用它们 。

神奇的是,它们是内置在JavaScript中的,你可以在任何库中使用它们。

有点晚了,但我用jQuery ajax方法。 但是我不想每秒都发一个请求,如果我还没有从最后的请求中得到回应,所以我做了这个。

 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(); 

没有pugin需要。你只能使用jquery。检查这个url

Jquery / Ajax调用定时器

你可以在javascript中使用setInterval()

 <script> //Call the yourAjaxCall() function every 1000 millisecond setInterval("yourAjaxCall()",1000); function yourAjaxCall(){...} </script> 

使用jQuery 每次插件。使用这个你可以做ajax调用“X”时间段

 $("#select").everyTime(1000,function(i) { //ajax call } 

你也可以使用setInterval

我发现一个非常好的jQuery插件,可以减轻这种types的操作的生活。 您可以结帐https://github.com/ocombe/jQuery-keepAlive

 $.fn.keepAlive({url: 'your-route/filename', timer: 'time'}, function(response) { console.log(response); });//