I have 2 function that should be called when the page is loading.
window.addEventListener("load", function(e) {
func_a(); //send request to server (Node js)
func_b(); //send request to server (Node js)
});
func_a will send to the server a request which just update list and will not return by .end.
func_b() should send a request to the server and notify to all responses in the list.
But, for some reason the request of func_b() sent before the another request.. and notify nothing actually.
Why is it happening?
Edit:
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();
}
Ajax works asynchronously, so It is not guaranteed to complete the first request before the second one. So you have to make the second request after the first one is finished by providing it as a callback function for the first request