简体   繁体   English

如何确保在前一个块完成后运行一个 JavaScript 块?

[英]How do I make sure a block of JavaScript runs after previous block has finished?

I have the following JavaScript code我有以下 JavaScript 代码

 function getWorkflowSchemeName(projectKey, callback){ var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey AJS.$.get(restCall, function(response){ if(response != null){ callback(response.name) } console.log("Im in here") }) } pairTypeValues = {} AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ pairTypeValues[value.innerText] = workflowSchemeName }) }) //The following code MUST run after ALL the pairTypeValues are recieved. 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){ console.log("SUP") AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") } }) counter++ })

I'm making numerous rest calls and saving the responses in PairTypeValues object.我进行了大量的休息调用并将响应保存在 PairTypeValues 对象中。 (it takes time to get all the data) (获取所有数据需要时间)

The last block of code is responsible for adding the data found in PairTypeValues.最后一段代码负责添加在 PairTypeValues 中找到的数据。

I have tried running the last block separately (instead of single file execution) and it runs fine, because till then all the values are stored in PairTypeValues object.我尝试单独运行最后一个块(而不是单个文件执行)并且它运行良好,因为在此之前所有值都存储在 PairTypeValues 对象中。 But when I run the code all together, it doesn't print anything.但是当我一起运行代码时,它不会打印任何内容。

I've tried doing it with adding another callback but it didn't work:我尝试通过添加另一个回调来做到这一点,但没有奏效:

 function getWorkflowSchemeName(projectKey, callback){ var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey AJS.$.get(restCall, function(response){ if(response != null){ callback(response.name) } console.log("Im in here") }) } function makingPairTypes(anotherCallback){ pairTypeValues = {} AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ anotherCallback(pairTypeValues[value.innerText] = workflowSchemeName) }) }) } counter = 1 AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){ makingPairTypes(function(secondCallback){ AJS.$.each(secondCallback, function(index,value){ if(val.innerText == index){ console.log("SUP") AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") } }) }) counter++ })

I have also tried using Deferred Methods, but that isn't working for me either:我也尝试过使用延迟方法,但这对我也不起作用:

 function makingPairTypes(){ var pairTypeValues = {} AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ pairTypeValues[value.innerText] = workflowSchemeName }) }) } //The following code MUST run after ALL the pairTypeValues are recieved. function addingSchemeNames(){ 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){ console.log("SUP") AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") } }) counter++ }) } var dm = AJS.$.Deferred(); dm.done([makingPairTypes, addingSchemeNames]);

I just want to make sure that all pairTypeValues are collected before the last block is executed.我只想确保在执行最后一个块之前收集了所有 pairTypeValues。

Can someone please help me out?有人可以帮我吗? I don't want to insert SetTimeOuts in the code.我不想在代码中插入 SetTimeOuts。

Many thanks in advance提前谢谢了

There are modern solutions like using Promises and Promise.all.有一些现代解决方案,例如使用 Promises 和 Promise.all。 But I think what you have there is actually only one piece of logic away from working.但我认为你所拥有的实际上离工作只有一个逻辑。 You just need to track the running number of responses, updating it in the response callback each time it's executed, and then when you have them all, fire off the final code function from within the response callback.您只需要跟踪响应的运行数量,在每次执行时在响应回调中更新它,然后当您拥有所有响应时,从响应回调中触发最终代码函数。

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++
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保一个脚本块在下一个块之前先运行? - How to make sure a block of Script runs first, before the next block? 如何在前一个函数完成其作业时才生成一个函数? - How do I make a function only cast when a previous function has finished its job? 如何确保我的 javascript 动画在提交按钮完成之前运行? - How do I make sure my javascript animation runs before submit button is done? 如何在Javascript中实现内存块? - How do I implement a memory block in Javascript? 如何在 jQuery 中编写此 JavaScript 块? - How do I write this JavaScript block in jQuery? 网站的javascript完成页面构建后,如何运行Chrome扩展程序内容脚本? - How do I run Chrome extension content script after a site's javascript has finished building page? 我怎样才能确保.animate在再次执行之前已经完成 - How can I make sure .animate has finished before performing again 如何让这个代码块在它之后的其他代码之前完成执行? - How do I make this code block to finish executing before other codes after it? 在先前完成之后,如何启动ajax请求? - how do I launch an ajax request after the prior on has finished? 如何仅在异步代码块完成后访问 JavaScript 中的全局变量 - How do I access global variable in JavaScript only after asynch code block completes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM