javascript 等待循环结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19715216/
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
Wait for loop to finish
提问by Tam2
Is there a way to make sure a for loop has finished before running the next function?
有没有办法在运行下一个函数之前确保 for 循环已经完成?
I have a scenario where the user is presented with a list of users, they can select an X number of these users and once they press 'Done' for each user that has been selected I call a REST API service to get some more information on the selected user to add to the 'users' array.
我有一个场景,用户会看到一个用户列表,他们可以选择 X 个这些用户,一旦他们为每个选择的用户按下“完成”,我就会调用 REST API 服务来获取更多信息要添加到“用户”数组的选定用户。
But what's happening is whatever I place after the for loop seems to run before it has finished and therefore there are users missing from it
但是发生的事情是我在 for 循环之后放置的任何东西似乎在它完成之前运行,因此有用户从中丢失
Sample code below:
示例代码如下:
function doCreateStory() {
var users = [];
// Add logged in user as creator
users.push({
"id" : user_id,
"creator" : true
});
// Add all checked users
for (var i = 0, len = items.length; i < len; i++) {
if (items[i].properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK) {
api.UserSearch({
"method" : "facebook",
"id" : items[i].properties.id
}, function(success, res, code) {
if (success == 1) {
users.push({
"id" : res.message._id,
"creator" : false
});
} else {
// Its broke..
}
});
}
}
// WANT TO DO SOMETHING HERE with 'users' array once loop has finished
}
回答by jeremy
api.UserSearch is an async function. You should keep track of the responses and when they have all come in, then handle the data returned.
api.UserSearch 是一个异步函数。您应该跟踪响应以及它们何时全部进入,然后处理返回的数据。
var requests = 0;
for (var i = 0, len = items.length; i < len; i++) {
if (items[i].properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK) {
requests++;
api.UserSearch({
"method" : "facebook",
"id" : items[i].properties.id
}, function(success, res, code) {
requests--;
if (success == 1) {
users.push({
"id" : res.message._id,
"creator" : false
});
} else {
// Its broke..
}
if (requests == 0) done();
});
}
}
function done() {
// WANT TO DO SOMETHING HERE with 'users' array once loop has finished
}
This will increment a counter requests
and when they have all come in, it should call the function done()
这将增加一个计数器requests
,当它们都进来时,它应该调用该函数done()
回答by SeinopSys
The problem lies in the fact that asyncrounus AJAX requests take time to complete. One way to handle this is by using a condition is your success handler:
问题在于异步 AJAX 请求需要时间才能完成。处理此问题的一种方法是使用条件是您的成功处理程序:
var completedRequests = 0;
// Add all checked users
for (var i = 0, len = items.length; i < len; i++) {
if (items[i].properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK) {
api.UserSearch({
"method" : "facebook",
"id" : items[i].properties.id
}, function(success, res, code) {
if (success == 1) {
completedRequests++;
users.push({
"id" : res.message._id,
"creator" : false
});
if (completedRequests === len){
//all ajax requests are finished
}
} else {
// Its broke..
}
});
}
}