使用Javascript:等待在循环功能来完成下一次迭代前执行 [英] Javascript: wait for function in loop to finish executing before next iteration

查看:515
本文介绍了使用Javascript:等待在循环功能来完成下一次迭代前执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(function() {
$("#submit").click(function() {


for (var i=1; i<=14; i++)
{
        setID(i); 
        checkField(i);

}

if ($('#pass_fail').val() != "fail")
{ 
//do something
}

这是当我点击一个登记表的提交按钮会发生什么。

This is what happens when I click on the submit button of a registration form.

执行顺序我想:

结果,SETID(1);
结果,校验场(1); //等待此完成
结果,SETID(2);
结果,校验场(2);
结果,SETID(3);
结果校验场(3);
结果....
结果if语句


setID(1);
checkField(1); //wait for this to finish
setID(2);
checkField(2);
setId(3);
checkField(3);
....
if statement

执行顺序,我得到:

结果,对循环从i = 1到i = 14去
结果if语句
结果校验场(我)■以随机顺序


for loop going from i=1 to i=14
if statement
checkField(i)s in random order

问题:$('#pass_fail)VAL()有通行证的默认值。然而通过循环校验场(i)向失败可能会改变它的值。但是,由于现实中的if语句之前校验场(我)■执行,值$('#pass_fail')总是最终被通行证和做一些事情始终执行。

Problem: $('#pass_fail').val() has a default value of 'pass'. However through the loop checkField(i) might change its value to 'fail'. But since in reality the if statement is executed before checkField(i)s, the value of $('#pass_fail') always ends up being 'pass' and the 'do something' always executes.

我如何等待校验场(i)向移动到循环的下一次迭代之前完成执行?谢谢你在前进!

How do I wait for checkField(i) to finish executing before moving on to the next iteration of the looP? Thank you in advance!

推荐答案

我是从你的问题说明了校验场()正在某种异步的假设Ajax调用。如果你真的想序列化到校验场调用()所以第二个呼叫是不是直到第一个已完成制作,那么你需要改变你的$的结构和流程为了C $ C至做到这一点。这样做的两种常用方法要么利用回调指示调用完成校验场()触发下一次调用校验场() 或使用的承诺来完成类似的东西。

I'm assuming from your description of the issue that checkField() is making some sort of asynchronous Ajax call. If you truly want to serialize the calls to checkField() so the 2nd call is not made until the first has completed, then you need to change the structure and flow of your code in order to do that. The two common ways of doing so are either using a callback that indicates completion of a call to checkField() that triggers the next call to checkField() or the use of promises to accomplish something similar.

下面是回调机制的一个例子:

Here's an example of the callback mechanism:

$("#submit").click(function() {
    var i = 0;
    function next() {
        i++;
        if (i <= 14) {
            setID(i);
            checkField(i, next);
        } else {
            // all checkField operations are done now
            if ($('#pass_fail').val() != "fail") { 
                //do something
            }
        }
    }
    next();
});

然后,校验场()将不得不进行修改,以调用传递给它的回调完成其异步操作的时候。

Then, checkField() would have to be modified to call the callback passed to it when it completes its asynchronous operation.

如果你正在使用jQuery做校验场里面的Ajax操作,这将是相当容易使用jQuery承诺也解决此问题。

If you're using jQuery to make the ajax operations inside of checkField, it would be fairly easy to use jQuery promises to solve this also.

这篇关于使用Javascript:等待在循环功能来完成下一次迭代前执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆