javascript 异步的替代方法:false ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22090764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alternative to async: false ajax
提问by rpsep2
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.
我循环遍历一个数组,为每个数组运行一个 ajax 请求。我需要按顺序发生请求,所以我可以选择最后一个请求并在成功时运行一个函数。
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
}
}
})
}
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.
但是,使用 async:false 会使应用程序无响应/缓慢。但是,如果没有 async:false 有时其中一个请求会挂起一点并在最后发送的 ajax 请求返回后实际返回。
how can i implement this without using async:false ?
我如何在不使用 async:false 的情况下实现这一点?
回答by jfriend00
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.
您可以使用本地函数来运行 ajax 调用,并且在每个连续的成功处理程序中,您可以启动下一个 ajax 调用。
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();
}
Updating this answer in 2016 with an option that uses promises. Here's how you run the requests in series:
在 2016 年使用使用承诺的选项更新此答案。以下是您如何按顺序运行请求:
array.reduce(function(p, item) {
return p.then(function() {
// you can access item.id and item.title here
return $.ajax({url: 'url here', ...}).then(function(result) {
// process individual ajax result here
});
});
}, Promise.resolve()).then(function() {
// all requests are done here
});
Here's how you run them in parallel, returning all the results:
以下是并行运行它们并返回所有结果的方法:
var promises = [];
array.forEach(function(item) {
// you can access item.id and item.title here
promises.push($.ajax({url: 'url here', ...});
});
Promise.all(promises).then(function(results) {
// all ajax calls done here, results is an array of responses
});
回答by Kevin
You can put your AJAX code in a separate function and whenever a request finishes it can call this method and pass its identity which will be incremented for the next request.
您可以将您的 AJAX 代码放在一个单独的函数中,每当请求完成时,它都可以调用此方法并传递其身份,该身份将为下一个请求递增。
回答by Arun P Johny
Try $.when()
试试 $.when()
var requests = $.map(array, function (i, item) {
return ajax_request(item.id, item.title, i);
});
$.when.apply($, requests).done(function(){
$.each(arguments, function(i, params){
var data = params[0];
//do your stuff
})
})
function ajax_request(id, title, i) {
return $.ajax({
async: false,
url: 'url here'
})
}