2016-10-23 4 views
0

당신이 http (s) 요청을 보낼 때 함수를 만들려고합니다. 진행중인 요청이 5 개 이상인 경우 중 하나가 완료 될 때까지 기다려야 다음 요청을 처리 할 수 ​​있습니다. 그 순수한 JS에서 할 수있는 방법이 지금까지자바 스크립트 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

이 방법이 유용합니까? 첫 번째 줄은 유효한 자바 스크립트처럼 보이지 않습니다. – 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

고맙습니다 ... –

관련 문제