2016-10-23 21 views
0

私はあなたがhttp(s)リクエストを送信するときに関数を作成しようとしています。 5つ以上の進行中のリクエストがある場合は、 が完了するまで待つ必要があり、次のリクエストを処理できます。 私は、それは純粋なJSで行うことができますどのようにこの今のところJavascript AJAX保留中のリクエスト

function makerequest ("/routeplanner/data","GET",sucess,error) 
{ 


var xmlHttp = new XMLhttpRequest(); 


var count =0; 

xmlHttp.onreadystatechange = function() { 
if (xmlHttp.readystate ==4 && xmlHttp.statue ==200) { 
success (xmlHttp.responseText);} 

else if (xmlHttp.status !=200 &&count !=3){ 
count++; 
xmlHttp.open(method,url,true); 

else if (count ==3){ 
error("you have reached the maximum number of tries") 
} 
} 
xmlHttp.open("GET","/routeplanner/data",true); 

xmlHttp.send(null) 
} 

任意のアイデアを行っています。

+1

これは機能しますか?最初の行は有効なjavascriptのようには見えません。 – Ouroborus

+0

いいえ、私はエラーがあります –

+0

あなたの質問にエラーを追加してください。 – Ouroborus

答えて

1

構文の例のコードでは、チェックの対象外のcountを追跡する必要があります。

// Store this outside of the function 
var activeRequests = 0; 
var maximumRequests = 5; 

function makeRequest(...) { 
    // Quit early if the maximum has been exceeded 
    if (activeRequests >= maximumRequests) { 
    error('too many requests'); 
    return; 
    } 
    ... 
    xmlHttp.onreadystatechange = function() { 
    ... 
    // Decrease the number of active requests when one is completed 
    --activeRequests; 
    ... 
    }; 
    ... 
    // Increase the number of active requests when one starts 
    ++activeRequests; 
    ... 
} 
+0

ありがとうございました... –

関連する問題