2016-09-20 4 views
-1

に残っている場合にも行われているかどうかを確認たとえば2つのAJAX処理はAJAXプロセスの残りは、私はすべてのAJAXプロセスは以下のコードによって行われているかどうかを検出する方法を既に知っている進捗

jQuery(document).ajaxStop(function(){ 
    alert('All AJAX Process is done.'); 
}); 

3つのAJAXプロセスが存在します:oneProcess,twoProcess,threeProcess

oneProcessおよびtwoProcessは既に完了しており、threeProcessはまだ処理中です。どのように私は2つのAJAXプロセスが完了したと呼びましたか?

+1

あなたはおそらく同様に – adeneo

+0

を行い、それらの2つのAJAX要求のためのコードを表示する必要があります 'oneProcess'はと' twoProcess'は常に 'threeProcess'前に終了しますか? – Terminus

+0

最初の2つのプロセスに対して、ajaxの 'complete'ローカルイベントを使用できます。 –

答えて

1

最も簡単なアプローチは、,twoProcessをパラメータとして使用して$.when()を利用することです。

var oneProcess = new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
    dfd.resolve("oneProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
}).then(log); 
 

 
var twoProcess = new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
    dfd.resolve("twoProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
}).then(log); 
 

 
var threeProcess = function(msg) { 
 
    log(msg); 
 
    return new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
     dfd.resolve("threeProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
    }) 
 
} 
 

 
function log(msg) { 
 
    console.log(msg); 
 
} 
 

 
var checkOneTwo = $.when(oneProcess, twoProcess); 
 

 
checkOneTwo.then(function(one, two) { 
 
    threeProcess(
 
    "oneProcess state:" + oneProcess.state() 
 
    + ", twoProcess state:" + twoProcess.state() 
 
).then(log) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script>

別の方法としては、両方のoneProcessをチェックするために、各繰延オブジェクトに.state()を呼び出すためsetIntervaltwoProcessdeferred.state()"resolved"を返すように呼び出して利用することができます。

var oneProcess = new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
    dfd.resolve("oneProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
}).then(log); 
 

 
var twoProcess = new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
    dfd.resolve("twoProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
}).then(log); 
 

 
var threeProcess = function(msg) { 
 
    log(msg); 
 
    return new $.Deferred(function(dfd) { 
 
    setTimeout(function() { 
 
     dfd.resolve("threeProcess complete") 
 
    }, Math.floor(Math.random() * 1500)); 
 
    return dfd.promise() 
 
    }) 
 
} 
 

 
function log(msg) { 
 
    console.log(msg); 
 
} 
 

 
var interval = null; 
 

 
var checkOneTwo = function() { 
 
    var oneTwo = new $.Deferred(); 
 
    interval = setInterval(function() { 
 
    console.log(oneProcess.state() === "resolved" 
 
       , twoProcess.state() === "resolved"); 
 
    if (oneProcess.state() === "resolved" && 
 
     twoProcess.state() === "resolved") { 
 
      clearInterval(interval); 
 
      oneTwo.resolve() 
 
    } 
 
    }, 100); 
 
    return oneTwo.promise().then(function() { 
 
    return threeProcess(
 
     `oneProcess state:${oneProcess.state()}` 
 
     + `twoProcess state:${twoProcess.state()}` 
 
    ) 
 
    }) 
 
} 
 

 
checkOneTwo().then(log);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
 
</script>

関連する問題