jQuery AJAX 间隔刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24494805/
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
AJAX Interval Refresh?
提问by bin0
I'm trying to make an AJAX function update about 30 seconds. I have a simple version of that done, here is the code.
我正在尝试使 AJAX 函数更新大约 30 秒。我有一个简单的版本,这是代码。
var refInterval = window.setInterval('update()', 30000); // 30 seconds
var update = function() {
$.ajax({
type : 'POST',
url : 'post.php',
success : function(data){
$('.voters').html(data);
},
});
};
This works, however, when the function is FIRST called I don't want it to wait 30 seconds, I just want the function to call, then wait 30 seconds, call again, wait 30 seconds, call again, etc. Any help?
但是,这是有效的,当函数第一次调用时,我不希望它等待 30 秒,我只想调用函数,然后等待 30 秒,再次调用,等待 30 秒,再次调用等等。有帮助吗?
回答by Alnitak
Consider using setTimeout
instead - it's more reliable. setInterval
timers can stack when the window doesn't have focus and then all run at once when it gets focus back again. Using setTimeout
also ensures that you don't get multiple AJAX requests queued up if the first one blocks for some reason.
考虑setTimeout
改用 - 它更可靠。 setInterval
当窗口没有焦点时计时器可以堆叠,然后当它再次获得焦点时立即运行。使用setTimeout
还可以确保如果第一个 AJAX 请求由于某种原因阻塞,您不会将多个 AJAX 请求排队。
To start the loop immediately, use an IIFE ("immediately invoked function expression") wrapped around the function:
要立即开始循环,请使用环绕函数的 IIFE(“立即调用的函数表达式”):
(function update() {
$.ajax({
... // pass existing options
}).then(function() { // on completion, restart
setTimeout(update, 30000); // function refers to itself
});
})(); // automatically invoke for first run
p.s. don't use string arguments to setInterval
or setTimeout
- just pass the function reference directly.
ps 不要将字符串参数用于setInterval
or setTimeout
- 直接传递函数引用。
回答by DavidT
Just call update right after you define it:
只需在定义后立即调用 update :
var refInterval = window.setInterval('update()', 30000); // 30 seconds
var update = function() {
$.ajax({
type : 'POST',
url : 'post.php',
success : function(data){
$('.voters').html(data);
},
});
};
update();
回答by Palmer
Call update once (on document ready) before calling it with the interval:
调用 update 一次(在文档准备好时),然后以时间间隔调用它:
update();
var refInterval = window.setInterval('update()', 30000);
回答by wbennett
Invoke the function before setting the timer.
在设置定时器之前调用该函数。
Like so:
像这样:
var update = function() {
$.ajax({
type : 'POST',
url : 'post.php',
success : function(data){
$('.voters').html(data);
},
});
};
update();
var refInterval = window.setInterval('update()', 30000); // 30 seconds
回答by john Smith
just put the setTimeout into your successhandler and it should work like charm
只需将 setTimeout 放入您的成功处理程序中,它就会像魅力一样工作
var update = function() {
$.ajax({
type : 'POST',
url : 'post.php',
success : function(data){
$('.voters').html(data);
var refInterval = window.setTimeout('update()', 30000); // 30 seconds
},
});
};
update()