2016-09-19 65 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

這正是我想要做的。謝謝。 –

相關問題