每 X 分钟调用一次 jQuery Ajax 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4930439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call jQuery Ajax Request Each X Minutes
提问by Shahin
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?
如何在特定时间段内调用 Ajax 请求?我应该使用 Timer Plugin 还是 jQuery 有这个插件?
回答by Ben
You can use the built-in javascript setInterval.
您可以使用内置的 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
回答by linslusa
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.
有点晚了,但我使用了 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();
回答by santosh singh
No plugin required. You can use only jquery.
不需要插件。您只能使用 jquery。
If you want to set something on a timer, you can use JavaScript's setTimeout
or setInterval
methods:
如果你想在计时器上设置一些东西,你可以使用 JavaScript 的setTimeout
或setInterval
方法:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
回答by Calvin Fan
you can use setInterval()
in javascript
你可以setInterval()
在javascript中使用
<script>
//Call the yourAjaxCall() function every 1000 millisecond
setInterval("yourAjaxCall()",1000);
function yourAjaxCall(){...}
</script>
回答by JMP
You have a couple options, you could setTimeout()
or setInterval()
. Here's a great article that elaborates on how to use them.
你有几个选择,你可以setTimeout()
或setInterval()
。 这是一篇很好的文章,详细说明了如何使用它们。
The magic is that they're built in to JavaScript, you can use them with any library.
神奇之处在于它们内置于 JavaScript,您可以将它们与任何库一起使用。
回答by Gowri
use jquery Every time Plugin .using this you can do ajax call for "X" time period
每次使用 jquery插件。使用此插件,您可以对“X”时间段进行 ajax 调用
$("#select").everyTime(1000,function(i) {
//ajax call
}
you can also use setInterval
你也可以使用setInterval
回答by Olotin Temitope
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.
我发现了一个非常好的 jquery 插件,它可以通过这种类型的操作让你的生活更轻松。您可以查看https://github.com/ocombe/jQuery-keepAlive。
$.fn.keepAlive({url: 'your-route/filename', timer: 'time'}, function(response) {
console.log(response);
});//