正如標題所說,我想發送$ http.get請求到服務器,直到返回的結果滿足條件。
這是我的嘗試:
var searchComplete = false;
var sendGetReq = true;
while(!searchComplete) // #2 and then jumps here
{
if(sendGetReq)
{
sendGetReq = false; // #1 execution reaches here
$http.get('/user/getCondition', {params: {condition: $scope.condition}).
success(function (condition, status, headers, config) {
...
...
if(condition)
{ searchComplete = true; }
else
{ sendGetReq = true; }
}
}
然而,$ http.get請求永遠不會。該聲明從未達到。當我調試時,我看到執行將會到達sendGetReq=false
,然後跳轉到while(!searchComplete)
。 GET請求永遠不會被髮送。
會有人請解釋:
- 爲什麼執行從未達到GET請求並跳回
- 什麼是做這種連續GET請求
謝謝你的推薦方式。
使用''.then''',而不是'''.success''',你應該使它成爲一個遞歸函數。不要期待一段時間和一起承諾的完美行爲。讓你自己成爲一旦這個承諾已經返回就自動調用的函數。 –