我有以下JavaScript代码
function getWorkflowSchemeName(projectKey, callback){
var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey
AJS.$.get(restCall, function(response){
if(response != null){
callback(response.name)
}
console.log("Im in here")
})
}
pairTypeValues = {}
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
pairTypeValues[value.innerText] = workflowSchemeName
})
})
//The following code MUST run after ALL the pairTypeValues are recieved.
counter = 1
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
AJS.$.each(pairTypeValues, function(index,value){
if(val.innerText == index){
console.log("SUP")
AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>")
}
})
counter++
})
&#13;
我正在进行大量的休息调用并在PairTypeValues对象中保存响应。 (获取所有数据需要时间)
最后一段代码负责添加PairTypeValues中的数据。
我已尝试单独运行最后一个块(而不是单个文件执行)并且运行正常,因为在此之前所有值都存储在PairTypeValues对象中。但是当我一起运行代码时,它并没有打印任何东西。
我尝试添加另一个回调,但它没有工作:
function getWorkflowSchemeName(projectKey, callback){
var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey
AJS.$.get(restCall, function(response){
if(response != null){
callback(response.name)
}
console.log("Im in here")
})
}
function makingPairTypes(anotherCallback){
pairTypeValues = {}
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
anotherCallback(pairTypeValues[value.innerText] = workflowSchemeName)
})
})
}
counter = 1
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
makingPairTypes(function(secondCallback){
AJS.$.each(secondCallback, function(index,value){
if(val.innerText == index){
console.log("SUP")
AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>")
}
})
})
counter++
})
&#13;
我也尝试使用延期方法,但这对我来说也不起作用:
function makingPairTypes(){
var pairTypeValues = {}
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
pairTypeValues[value.innerText] = workflowSchemeName
})
})
}
//The following code MUST run after ALL the pairTypeValues are recieved.
function addingSchemeNames(){
counter = 1
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
AJS.$.each(pairTypeValues, function(index,value){
if(val.innerText == index){
console.log("SUP")
AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>")
}
})
counter++
})
}
var dm = AJS.$.Deferred();
dm.done([makingPairTypes, addingSchemeNames]);
&#13;
我只想确保在执行最后一个块之前收集所有pairTypeValues。
有人可以帮帮我吗? 我不想在代码中插入SetTimeOuts。
非常感谢提前
答案 0 :(得分:1)
有使用Promises和Promise.all等现代解决方案。但我认为你所拥有的实际上只有一条逻辑远离工作。您只需要跟踪响应的运行数量,每次执行时都在响应回调中更新它,然后当您拥有它们时,从响应回调中触发最终的代码函数。
function getWorkflowSchemeName(projectKey, callback){
var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey;
AJS.$.get(restCall, function(response){
callback(response);
})
};
pairTypeValues = {};
doneIfZero = AJS.$(".projects-list tr td:nth-child(3)").length;
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
getWorkflowSchemeName(value.innerText, function(response){
var workflowSchemeName = response.name;
if(workflowSchemeName != null){
pairTypeValues[value.innerText] = workflowSchemeName;
}
doneIfZero--;
if (doneIfZero === 0) {
codeToRunAfterAllResponsesAreBack()
}
});
});
//The following code MUST run after ALL the pairTypeValues are recieved.
function codeToRunAfterAllResponsesAreBack(){
counter = 1
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
AJS.$.each(pairTypeValues, function(index,value){
if(val.innerText == index){
AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>")
}
});
counter++
});
};