[英]How do I make sure a block of JavaScript runs after previous block has finished?
I have the following JavaScript code我有以下 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++ })
I'm making numerous rest calls and saving the responses in PairTypeValues object.我进行了大量的休息调用并将响应保存在 PairTypeValues 对象中。 (it takes time to get all the data) (获取所有数据需要时间)
The last block of code is responsible for adding the data found in PairTypeValues.最后一段代码负责添加在 PairTypeValues 中找到的数据。
I have tried running the last block separately (instead of single file execution) and it runs fine, because till then all the values are stored in PairTypeValues object.我尝试单独运行最后一个块(而不是单个文件执行)并且它运行良好,因为在此之前所有值都存储在 PairTypeValues 对象中。 But when I run the code all together, it doesn't print anything.但是当我一起运行代码时,它不会打印任何内容。
I've tried doing it with adding another callback but it didn't work:我尝试通过添加另一个回调来做到这一点,但没有奏效:
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++ })
I have also tried using Deferred Methods, but that isn't working for me either:我也尝试过使用延迟方法,但这对我也不起作用:
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]);
I just want to make sure that all pairTypeValues are collected before the last block is executed.我只想确保在执行最后一个块之前收集了所有 pairTypeValues。
Can someone please help me out?有人可以帮我吗? I don't want to insert SetTimeOuts in the code.我不想在代码中插入 SetTimeOuts。
Many thanks in advance提前谢谢了
There are modern solutions like using Promises and Promise.all.有一些现代解决方案,例如使用 Promises 和 Promise.all。 But I think what you have there is actually only one piece of logic away from working.但我认为你所拥有的实际上离工作只有一个逻辑。 You just need to track the running number of responses, updating it in the response callback each time it's executed, and then when you have them all, fire off the final code function from within the response callback.您只需要跟踪响应的运行数量,在每次执行时在响应回调中更新它,然后当您拥有所有响应时,从响应回调中触发最终代码函数。
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++
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.