2015-05-04 122 views
3

我正在使用以下代碼對ajax請求進行排隊,而不是阻止頁面上的操作。 問題是,我需要顯示一個加載圖標來指示隊列的執行沒有完成。我無法弄清楚如何去做,而我試過的所有東西都會立即關閉。ajaxQueue加載圖標

// jQuery on an empty object, we are going to use this as our Queue 
    var ajaxQueue = $jq({}); 

    $jq.ajaxQueue = function(ajaxOpts) { 
    var jqXHR, 
     dfd = $jq.Deferred(), 
     promise = dfd.promise(); 

    // queue our ajax request 
    ajaxQueue.queue(doRequest); 

    // add the abort method 
    promise.abort = function(statusText) { 
     // proxy abort to the jqXHR if it is active 
     if (jqXHR) { 
      return jqXHR.abort(statusText); 
     } 
     // if there wasn't already a jqXHR we need to remove from queue 
     var queue = ajaxQueue.queue(), 
      index = $jq.inArray(doRequest, queue); 

     if (index > -1) { 
      queue.splice(index, 1); 
     } 
     // and then reject the deferred 
     dfd.rejectWith(ajaxOpts.context || ajaxOpts, 
      [ promise, statusText, "" ]); 
     return promise; 
    }; 

    // run the actual query 
    function doRequest(next) { 
     jqXHR = $jq.ajax(ajaxOpts) 
      .done(dfd.resolve) 
      .fail(dfd.reject) 
      .then(next, next); 
    } 
    return promise; 
}; 

回答