2015-04-02 57 views
0

我有一套getJson()函數,我連續使用jQuery的推遲調用。jQuery推遲:條件未驗證時停止進度

function f1(){ $.getJSON(..)} 
function f2(){ $.getJSON(..)} 
.... 
function fn(){ $.getJSON(..)} 

var dfd = $.Deferred(); 
dfd.done(f1,f2...fn) 

dfd.resolve(); 

的事情是,該功能的一個可能會遇到一個給定的問題,並返回,在這種情況下,我想停步的進取,而不是調用其餘功能。

我怎樣才能做到這一點?

回答

0

的兩種方法可以做到這一點是

1.如果反應不依賴於彼此

var req1 = $.getJSON(...).then(function(response1) { 
    //evaluate the response and either reject or resolve a Deferred based on the results of your evaluation 
    if(!response1) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response1); 
}); 

var req2 = $.getJSON(...).then(function(response2) { 
    //evaluate the response 
    if(!response1) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response2); 
}); 

var req3 = $.getJSON(...).then(function(response3) { 
    //evaluate the response 
    if(!response1) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response3); 
}); 

var req4 = $.getJSON(...).then(function(response4) { 
    //evaluate the response 
    if(!response1) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response4); 
}); 

$.when(req1,req2,req3,req4) 
    .then(
    function(response1,response2,response3,response4) { /* called when all requests are successfully complete */ }, 
    function() { /* called when any of the requests have failed */ } 
    ); 

2.如果響應DO互相依賴

$.getJSON(...) //req1 

.then(function(response1) { 

    //evaluate response1 
    if(!response1) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response1); 

}) 
.then(function(response1) { 

    return $.getJSON(...).then(function(response2) { 
    //evaluate the response 
    if(!response2) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response1,response2); 
    }); 

}) 
.then(function(response1,response2) { 

    return $.getJSON(...).then(function(response3) { 
    //evaluate the response 
    if(!response2) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response1,response2,response3); 
    }); 

}) 
.then(function(response1,response2,response3) { 

    return $.getJSON(...).then(function(response4) { 
    //evaluate the response 
    if(!response4) { return $.Deferred().reject(); } 
    return $.Deferred().resolve(response1,response2,response3,response4); 
    }); 

}) 
.then(

    function(response1,response2,response3,response4) { 
    //only called on success of all requests 
    }, 

    function() { 
    //called on failure of any individual request, or if the responses of any individual request did not meet your conditions 
    } 
) 

編輯

如果你在一個函數執行這些,那麼你只需要返回的承諾,並將其鏈接到受.then像這樣:

function makeReqs() { 
    //method 1 
    return $.when(req1,req2,req3,req4); 

    //method 2 
    return $.getJSON(...) 
     .then(...) 
     .then(...) 
     .then(...) 


} 

//call the function 
makeReqs 
    .then(
    function() { /* this is called on success */ }, 
    function() { /* this is called on failure */ } 
    ) 
+0

.pipe已被棄用的jQuery 1.8 – peinearydevelopment 2015-04-02 18:05:27

+0

@peinearydevelopment的 - 感謝,我最近一直在使用舊版本。應該使用'$ .then'我會編輯。 – Adam 2015-04-02 18:06:35

+0

謝謝,但我如何觸發成功或失敗的功能? – MahmoudKsemtini 2015-04-02 18:06:48