有與指數沒有問題,
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