开发者

How do I make Ajax update every 10 seconds in jquery?

How do I make Ajax update every 10 seconds in jquery?

    $.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 th开发者_如何学JAVAe RSS every 10 seconds so the user can see a new item in the feed?


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


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;
        ...
      }
    });
  }
};


wrap it in a function and use setInterval()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜