我想编写以下 JS 代码,但我不确定它是否可行 - 它会比下面的代码复杂得多,我希望在浪费大量时间之前得到其他人的建议。
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 element of array - ajax_request(array_element)
//Does the loop wait for the ajax request to be completed to come
}
从上面,循环会等待 AJAX 请求还是继续?如果它继续,我怎样才能让它等待?如果没有办法做到这一点,我是否应该重新考虑我的逻辑?!
感谢任何帮助。
最佳答案
由于默认情况下 AJAX 请求是异步的,因此循环不会等待。如果您需要等待调用结果再继续,则应使用 AJAX 请求的回调函数。我强烈建议不要使请求同步,因为这会阻塞客户端,直到请求完成。
关于javascript - 此 JS 代码是否等待 AJAX 请求完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1782408/