2013-06-20 17 views
0

在我的項目中,我必須使用jQuery 1.4.x.我有四個異步$ .ajax函數觸發,所有返回true/false。我需要等待在IF中對他們進行評估,因爲如果我現在這樣做,並非所有人都完成了射擊。

代碼(所有在$(文件)。就緒..:`

function getHTTPStatusCode200(urlIn) { 
     $.ajax({ 
      url: urlIn, 
      complete: function (xhr) { 
       return (xhr.status == 200); 
      } 
     }); 
    } 

還有那些四,我需要做這樣的事情:

if(getHTTPStatusCode200(url) && getHTTPStatusCode401(url)){ 
    alert("It worked."); 
} 

`

+0

如何在完整的處理程序內,看起來像是一個很好的地方堅持條件。 – adeneo

+0

因爲你是jQuery的舊版本,如果你按順序運行它們,建議你在每個Ajax調用中使用'async:false'。請注意'async'會阻止你的瀏覽器,直到完成'ajax'爲止 – krishgopinath

+0

這就是* when *的意思。爲什麼這不是一個選項?它在1.4沒有? –

回答

2

$.when概念並不難模仿:

var results = []; 
function newResult(result) { 
    results.push(result); 
    if (results.length === 4) { 
     alert("It worked."); 
    } 
} 

function getHTTPStatusCode200(urlIn) { 
    $.ajax({ 
     url: urlIn, 
     complete: function (xhr) { 
      if (xhr.status === 200) { 
       newResult(xhr); 
      } 
     } 
    }); 
} 

// your other functions, each calling newResult when done 
+0

真棒...作品像一個魅力 –

-1

添加async:false,像這樣:

$.ajax({ 
     url: urlIn, 
     async: false, 
     complete: function (xhr) { 
      return (xhr.status == 200); 
     } 
    }); 
+3

你仍然不能退出這樣的回調,而異步:false是一種不好的做法。 –

+2

這是要走的路......如果你試圖鎖定瀏覽器並惹惱你的用戶。 – adeneo

+0

你們正在假設一個使用率很高的應用程序,每個ajax調用的處理時間都相對較長。 OP最好判斷這一點。在我的項目中,有幾個地方使用'async:false'是有幫助的,並且不會以任何人類可能注意到的方式影響用戶的體驗 - 即使使用秒錶。每個項目都不一樣。 – gibberish

1

僅僅因爲你沒有延期對象並不意味着你不能使用回調。

function getHTTPStatusCode200(urlIn,callback) { 
    $.ajax({ 
     url: urlIn, 
     complete: function (xhr) { 
      callback(xhr.status) 
     } 
    }); 
} 

getHTTPStatusCode200("foo.php",function(status){ 
    alert(status); 
}); 
0

非阻塞解決方案,一樣快是通過成功回調異步而是順序給他們打電話:

定義你的方法以便它返回ajax對象。

function getHTTPStatusCode200(urlIn) { 
    return $.ajax({ 
     url: urlIn 
    }); 
} 

然後把它們連:

getHTTPStatusCode200(url1).complete(function(xhr1){ 
    getHTTPStatusCode200(url2).complete(function(xhr2){ 
     getHTTPStatusCode200(url3).complete(function(xhr3){ 
      getHTTPStatusCode200(url4).complete(function(xhr4){ 
       //all requests have been completed and you have access to all responses 
      } 
     } 
    } 
} 

這方面的工作作爲直接替代$。當:

$.when = function() { 
    this.requests = []; 
    this.completeFunc; 
    this.interval; 

    //if you want it to work jsut like $.when use this 
    this.requests = arguments; 
    //If you want to pass in URLs use this 
    //for(var i in arguments) { 
    // this.requests.push($.get(arguments[i])); 
    //}; 

    this.complete = function(func) { 
     this.completeFunc = func; 
     var when = this; 
     this.interval = window.setInterval(function(){ 
      checkReadyStates(when) 
     }, 100); 
     return this; 
    }; 

    var checkReadyStates = function(when) { 
     var complete = 0; 
     for(var i in when.requests) { 
      if(when.requests[i].readyState==4) { 
       complete++; 
      } 
     } 
     if(complete == when.requests.length) { 
      window.clearInterval(when.interval); 
      when.completeFunc.apply(when, when.requests); 
     } 
    }; 

    return this; 
} 

function handler(){ 
    console.log('handler'); 
    console.log(arguments); 
    //The arguments here are all of the completed ajax requests 
    //see http://api.jquery.com/jQuery.ajax/#jqXHR 
} 

$.when(xhr1,xhr2,xhr3).complete(handler); 
//or 
//$.when('index.php','index2.php','index3.php').complete(handler); 

現在和工程測試。