페이지를로드 할 때 호출해야하는 함수가 2 개 있습니다. 다른 요청을 보내기 전에 기다려주십시오. [자바 스크립트]
window.addEventListener("load", function(e) {
func_a(); //send request to server (Node js)
func_b(); //send request to server (Node js)
});
func_a
.end
에 의해 반환하지 않습니다 요청을 보낼 것입니다.
func_b()
의 요청이 실제로 아무것도 있습니다 .. 다른 요청하기 전에 전송되지 및 통지 어떤 이유로
func_b() should send a request to the server and notify to all responses in the list.
하지만.
왜 그런가?
편집 :
function func_a() {
var xhttp = new XMLHttpRequest();
xhttp.addEventListener('load', function (e) {
if(xhttp.status != 200){
//handle error
return;
}
handle_func(this.responseText);
func_a();
});
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
좋은 아이디어는 체인 약속하는 것입니다 약속을 반환하면 ES7에 익숙하다면
async-await
의 땅이다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 그러면 then() 메소드를 사용하여 요청을 차례로 보낼 수 있습니다. –