替代异步:假阿贾克斯 [英] alternative to async: false ajax
问题描述
通过运行一个Ajax请求的每个阵列我循环。 我需要请求的情况发生,从而,这样我就可以拿起最后一个请求,并运行成功的功能。
I loop through an array running an ajax request for each. I need the requests to happen in order, so i can pick up the last request and run a function on success.
目前进出口运行(简体):
at the moment im running (simplified):
$.each(array, function(i, item){
ajax_request(item.id, item.title, i);
})
function ajax_request(id, title, i){
$.ajax({
async: false,
url: 'url here',
success: function(){
if(i == array.length-1){
// run function here as its the last item in array
}
}
})
}
然而,使用异步:假使应用程序反应迟钝/慢。 但是,如果没有异步:假的,有时一个请求将挂了一下,最后发送Ajax请求返回后,竟返回
however, using async:false makes the application unresponsive/ slow. but, without async:false sometimes one of the requests will hang a bit and actually return after the last sent ajax request returns.
我如何能实现这一点没有使用异步:假
how can i implement this without using async:false ?
推荐答案
您可以使用本地函数运行Ajax调用,并在每个连续的成功处理,你可以揭开序幕,接下来的AJAX调用。
You can use a local function for running the ajax call and in each successive success handler, you can kick off the next ajax call.
function runAllAjax(array) {
// initialize index counter
var i = 0;
function next() {
var id = array[i].id;
var title = array[i].title;
$.ajax({
async: true,
url: 'url here',
success: function(){
++i;
if(i >= array.length) {
// run function here as its the last item in array
} else {
// do the next ajax call
next();
}
}
});
}
// start the first one
next();
}
这篇关于替代异步:假阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!