有与指数没有问题,
Ajax响应时间正好是不同的,所以它不会是为了
像这样:
function getChildContent (capname, callback) {
// simulate ajax call with random response time
setTimeout(function() {
var content='pizza';
callback(content);
}, (Math.random(0,1000)*1000));
}
var parent = { Functions: [] };
var caps = ['lol', 'lol2', 'haha'];
for(var y = 0; y < caps.length; y++) {
(function(index) {
console.log(index);
getChildContent(caps[index], function(content) {
var child = {};
child.FunctionName = caps[index];
child.Content = [];
child.Content.push(content);
parent.Functions.push(child);
console.log(content, index, caps[index]);
});
})(y);
}
// Outputs:
// These are the indexes (Yes, they are in order)
// 0
// 1
// 2
// These are the responses (The order is based from the request completion time)
// pizza 1 lol2
// pizza 2 haha
// pizza 0 lol
[更新]
有一个变通,使你所希望的方式,但它会等待,直到所有的响应结束。
function waitLoadCompleted(callback) {
if(loaded == caps.length) {
// do stuff
callback();
}
}
function getChildContent (capname, callback) {
// simulate ajax call with random response time
setTimeout(function() {
var content='pizza';
callback(content);
}, (Math.random(0,1000)*1000));
}
var loaded = 0;
var tempcontainer = [];
var parent = { Functions: [] };
var caps = ['lol', 'lol2', 'haha'];
for(var y = 0; y < caps.length; y++) {
(function(index) {
console.log(index);
getChildContent(caps[index], function(content) {
loaded++;
tempcontainer.push({ index: index, data: content });
waitLoadCompleted(function(){
// everything is loaded
tempcontainer.sort(function(a, b) { return a.index - b.index; });
for(i in tempcontainer) {
var index = tempcontainer[i].index;
var child = {};
child.FunctionName = caps[index];
child.Content = [];
child.Content.push(content);
parent.Functions.push(child);
console.log(content, index, caps[index]);
}
})
});
})(y);
}
// Outputs:
// 0
// 1
// 2
// pizza 0 lol
// pizza 1 lol2
// pizza 2 haha
[UPDATE]
另一个解决方案是段到一个组的响应和使它看起来像everthings为了通过排序所收集的响应 例如像FB的帖子,聊天等类型,其中反应永远不会结束
function getChildContent (capname, callback) {
// simulate ajax call with random response time
setTimeout(function() {
var content='pizza';
callback(content);
}, (Math.random(0,1000)*1000));
}
var tempcontainer = [];
var parent = { Functions: [] };
var caps = ['lol', 'lol2', 'haha'];
for(var y = 0; y < caps.length; y++) {
(function(index) {
console.log(index);
getChildContent(caps[index], function(content) {
tempcontainer.push({ index: index, data: content });
tempcontainer.sort(function(a, b) { return a.index - b.index; });
for(i in tempcontainer) {
var child = {};
child.FunctionName = caps[index];
child.Content = [];
child.Content.push(content);
parent.Functions.push(child);
console.log(content, tempcontainer[i].index, caps[tempcontainer[i].index]);
}
console.log("<--SET RESPONSE-->");
});
})(y);
}
// Outputs:
// 0
// 1
// 2
// pizza 1 lol2
// <--SET RESPONSE-->
// pizza 0 lol
// pizza 1 lol2
// <--SET RESPONSE-->
// pizza 0 lol
// pizza 1 lol2
// pizza 2 haha
// <--SET RESPONSE-->
你如何检查是否'index'是相同的(如在它的面前,这个代码似乎是正确的)?如果你在另一个'console.log'旁边添加一个'console.log(index)',你是否看到相同的'index'? –
你也可以显示getChildContent()吗?也许它总是存储传递函数在同一个对象,那么,你覆盖在每个迭代函数...所以你总是与最后一个函数结束了......只是一个猜测... – treeno
@treeno我编辑的OP和添加的getChildContent()函数 – dnks23