Does this JS code wait for AJAX request to complete?
I want to write the following JS code but I am not sure if it will work - its going to be a lot more complicated than the below and I was hoping to get advice from others before I waste a lot of time.
function ajax_request(array_element){
$.get("process_txt.php", { path: full_path },
function (data){
});
}
function bulk_upload(){
//loop through array
//Call ajax function on one el开发者_如何学编程ement of array - ajax_request(array_element)
//Does the loop wait for the ajax request to be completed to come
}
From the above, will the loop wait for the AJAX request or just continue? If it continues, how can I get it to wait? If there is not a way to do this, shall I re-think my logic?!
I appreciate any help.
Since by default an AJAX request is asynchronous, the loop will not wait. The callback function of the AJAX request should be used if you need to wait for the results of the call before continuing. I would highly recommend NOT making the request synchronous as this will block the client side until the request completes.
i think you should split the "bulk_upload" function in two others. The first function do the ajax request and the second is called for the "callback" function of ajax.
luck!
精彩评论