私は、多くの残りの呼び出しを行うとPairTypeValuesオブジェクトに応答を保存しています次の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オブジェクトに格納されるまで、(単一ファイルの実行ではなく)最後のブロックを別々に実行しようとしましたが、正常に動作します。しかし、コードをまとめて実行すると、何も印刷されません。
私は別のコールバックを追加することでそれをやってみましたが、それはうまくいきませんでした:
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を挿入したくありません。
多くのおかげで、事前に
使用[生命維持(https://en.wikipedia.org/wiki/Immediately-invoked_function_expression)および[約束](https://developer.mozilla.org/en/docs/Web/JavaScript/参照/ Global_Objects/Promise) –
iifeも動作しませんでした。約束のために 、私がしようとしている: するvar F1 =新しい約束(funciton(履行を、拒否){ 第二のブロック の//コード}) 、その後 f1.then(*最後のブロック*のコード) これも機能しませんでしたか? 私はそれを正しくしていますか? –
あなたは約束をどこで解決していますか? –