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

前端 未结 3 1696
傲寒
傲寒 2021-01-14 16:08

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

    $.ajax({
    type: \"GET\",
    url: options.feedUrl,
    dataType: \"xml\",
    async:options.sync         


        
相关标签:
3条回答
  • 2021-01-14 16:27

    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);
    
    0 讨论(0)
  • 2021-01-14 16:27

    wrap it in a function and use setInterval()

    0 讨论(0)
  • 2021-01-14 16:35

    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;
            ...
          }
        });
      }
    };
    
    0 讨论(0)
提交回复
热议问题