我有以下的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(*最後一塊*的代碼) 這也沒用嗎? 我在做對吧? –
您是否在任何地方解決承諾? –