我有以下的JavaScript代码如何在上一个块完成后确保JavaScript块运行?
function getWorkflowSchemeName(projectKey, callback){
\t var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey
\t AJS.$.get(restCall, function(response){
\t \t if(response != null){
\t \t \t callback(response.name)
\t \t }
\t \t console.log("Im in here")
\t })
}
pairTypeValues = {}
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
\t
\t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
\t \t pairTypeValues[value.innerText] = workflowSchemeName
\t \t })
})
//The following code MUST run after ALL the pairTypeValues are recieved.
counter = 1 \t
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
\t AJS.$.each(pairTypeValues, function(index,value){
\t \t if(val.innerText == index){
\t \t \t console.log("SUP")
\t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t
\t \t }
\t })
\t counter++
})
我做了许多休息的呼叫和保存响应中PairTypeValues对象。 (获取所有数据需要时间)
最后一段代码负责添加在PairTypeValues中找到的数据。
我已经尝试单独运行最后一个块(而不是单个文件执行),它运行良好,因为直到那时所有的值都存储在PairTypeValues对象中。但是当我一起运行代码时,它不会打印任何内容。
我试着加入另一个回调这样做,但没有奏效:
function getWorkflowSchemeName(projectKey, callback){
\t var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey
\t AJS.$.get(restCall, function(response){
\t \t if(response != null){
\t \t \t callback(response.name)
\t \t }
\t \t console.log("Im in here")
\t })
}
function makingPairTypes(anotherCallback){
\t pairTypeValues = {}
\t AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
\t
\t \t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
\t \t \t anotherCallback(pairTypeValues[value.innerText] = workflowSchemeName)
\t \t \t })
\t })
}
\t counter = 1 \t
\t AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
\t \t makingPairTypes(function(secondCallback){
\t \t AJS.$.each(secondCallback, function(index,value){
\t \t \t if(val.innerText == index){
\t \t \t \t console.log("SUP")
\t \t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t
\t \t \t }
\t \t })
\t \t })
\t \t counter++
\t })
我也使用延迟的方法试过了,但就是不为我工作或者:
function makingPairTypes(){
var pairTypeValues = {}
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){
\t
\t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){
\t \t pairTypeValues[value.innerText] = workflowSchemeName
\t \t })
})
}
//The following code MUST run after ALL the pairTypeValues are recieved.
function addingSchemeNames(){
counter = 1 \t
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){
\t AJS.$.each(pairTypeValues, function(index,value){
\t \t if(val.innerText == index){
\t \t \t console.log("SUP")
\t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t
\t \t }
\t })
\t counter++
})
}
var dm = AJS.$.Deferred();
dm.done([makingPairTypes, addingSchemeNames]);
我只想确保在执行最后一个块之前收集所有的pairTypeValues。
有人可以帮我吗? 我不想在代码中插入SetTimeOuts。
很多感谢
使用[iife](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression)和[承诺](https://developer.mozilla.org/en/docs/Web/JavaScript/参考/ Global_Objects/Promise) –
iife也没有工作。 对于承诺,我想: VAR F1 =新的承诺(功能可按(履行,拒绝){// 典第二块 的}) 然后 f1.then(*最后一块*的代码) 这也没用吗? 我在做对吧? –
您是否在任何地方解决承诺? –