2016-09-19 66 views
1

我有以下的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。

很多感谢

+1

使用[iife](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression)和[承诺](https://developer.mozilla.org/en/docs/Web/JavaScript/参考/ Global_Objects/Promise) –

+0

iife也没有工作。 对于承诺,我想: VAR F1 =新的承诺(功能可按(履行,拒绝){// 典第二块 的}) 然后 f1.then(*最后一块*的代码) 这也没用吗? 我在做对吧? –

+0

您是否在任何地方解决承诺? –

回答

1

有喜欢使用的承诺和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++ 
    }); 
}; 
+0

注意:我不确定你为什么要使用counter,而不是从外部循环来的'ind',但是我已经把所有的代码'原样'不影响异步执行顺序问题。 –

+0

这正是我想要做的。谢谢。 –

相关问题