How do I make Ajax update every 10 seconds in jquery?
$.ajax({
type: \"GET\",
url: options.feedUrl,
dataType: \"xml\",
async:options.sync
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);
wrap it in a function and use setInterval()
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:
var doingAjax = false;
setInterval(function() {
if (!doingAjax) {
doingAjax = true;
jQuery.ajax({
...,
success: function() {
doingAjax = false;
...
}
});
}
};