How do I make Ajax update every 10 seconds in jquery?
如何在jquery中每10秒更新一次Ajax?
$.ajax({
type: "GET",
url: options.feedUrl,
dataType: "xml",
async:options.sync,
success: function(xml) {
}
For example I am testing the jquery above to get a RSS feed. So how do you make it update the RSS every 10 seconds so the user can see a new item in the feed?
例如,我正在测试上面的jquery以获取RSS提要。那么如何让它每10秒更新一次RSS,以便用户可以在Feed中看到新项目?
3 个解决方案
#1
6
Creating an interval
创建间隔
var ResInterval = window.setInterval('myAjaxCall()', 60000); // 60 seconds
var myAjaxCall = function() {
$.ajax({
type: "GET",
url: options.feedUrl,
dataType: "xml",
async:options.sync,
success: function(xml) {
// todo
}
};
To stop
window.clearInterval(ResInterval);
#2
1
I'd avoid the synchronous ajax call. It'll make anything else on the page like animations freeze. Of course with asynchronous calls you'll need to make sure they don't start overlapping. In your setInterval you could just put a lock:
我会避免同步ajax调用。它会在页面上做任何其他动画,就像动画冻结一样。当然,对于异步调用,您需要确保它们不会重叠。在你的setInterval中你可以放一把锁:
var doingAjax = false;
setInterval(function() {
if (!doingAjax) {
doingAjax = true;
jQuery.ajax({
...,
success: function() {
doingAjax = false;
...
}
});
}
};
#3
0
wrap it in a function and use setInterval()
将它包装在一个函数中并使用setInterval()
#1
6
Creating an interval
创建间隔
var ResInterval = window.setInterval('myAjaxCall()', 60000); // 60 seconds
var myAjaxCall = function() {
$.ajax({
type: "GET",
url: options.feedUrl,
dataType: "xml",
async:options.sync,
success: function(xml) {
// todo
}
};
To stop
window.clearInterval(ResInterval);
#2
1
I'd avoid the synchronous ajax call. It'll make anything else on the page like animations freeze. Of course with asynchronous calls you'll need to make sure they don't start overlapping. In your setInterval you could just put a lock:
我会避免同步ajax调用。它会在页面上做任何其他动画,就像动画冻结一样。当然,对于异步调用,您需要确保它们不会重叠。在你的setInterval中你可以放一把锁:
var doingAjax = false;
setInterval(function() {
if (!doingAjax) {
doingAjax = true;
jQuery.ajax({
...,
success: function() {
doingAjax = false;
...
}
});
}
};
#3
0
wrap it in a function and use setInterval()
将它包装在一个函数中并使用setInterval()