简体   繁体   English

等待函数完成,然后再if()else()

[英]Wait for a function to finish before if()else()

I have this: 我有这个:

var myError;

function getNames(user,location){
    myError=null;

    //Make an Ajax Call (this takes some time)
    //On Success do this:
        if(//Some Condition){
            myError=false;
        }else{
            myError=true
        }
}

function first(){
    getNames(user, location);

    if(myError==false){ //MYCONDITION
        //do some custom stuff just for first();
    }else{
        alert("Failed");
    }
}

function second(){
    getNames(user, location);

    if(myError==false){ //MYCONDITION
        //do some custom stuff just for second();
    }else{
        alert("Failed");
    }
}

Now, when I call first() or second() it always gives me alert message "FAILED". 现在,当我调用first()或second()时,它总是给我警报消息“ FAILED”。 The problem is that //MYCONDITION is being executed before the getNames() function finishes. 问题在于/// MYCONDITION在getNames()函数完成之前正在执行。 I want to wait for getNames() to execute before I can check //MYCONDITION. 我想等待getNames()执行,然后才能检查// MYCONDITION。

I cannot set a delay since I don't know how much time getNames() is going to take. 我无法设置延迟,因为我不知道getNames()将花费多少时间。 I was thinking about using some jQquery function but I cannot seem to find one. 我正在考虑使用一些jQquery函数,但似乎找不到一个。

And, I am trying to keep the getNames() as generic as possible. 而且,我正在尝试使getNames()尽可能通用。 So, I am thinking of a way to not disturb the getNames(). 因此,我正在考虑一种不打扰getNames()的方法。

The last option I see is adding callback() to getNames(). 我看到的最后一个选项是将callback()添加到getNames()。 But, this function is being called by other functions too that won't need callback(). 但是,该函数也被其他不需要回调()的函数调用。

Any thoughts guys? 有什么想法吗?

Thanks. 谢谢。

It's totally fine adding a callback; 添加回调完全没问题; you can just check if it exists: 您可以检查它是否存在:

function getNames(user, location, callback){
    myError=null;

    //Make an Ajax Call (this takes some time)
    //On Success do this:
        if(//Some Condition){
            myError=false;
        }else{
            myError=true
        }

        if(callback) {
           callback.apply();
        }
}

If you're bothered by the arity not being generic enough, you can have the function accept an object instead of arguments, so: 如果由于通用性不够而困扰,则可以让该函数接受对象而不是参数,因此:

function getNames(options) {
    var user = options.user;
    var location = options.loacation;
    var callback = options.callback;

    myError=null;

    //Make an Ajax Call (this takes some time)
    //On Success do this:
        if(//Some Condition){
            myError=false;
        }else{
            myError=true
        }

        if(typeof callback !== "undefined") {
           callback.apply();
        }
}

And when you call getNames , you can do: 当调用getNames ,您可以执行以下操作:

getNames({
    user: "some user",
    location: "some location",
    callback: function() {
        ...
    }
});

or 要么

getNames({
    user: "some user",
    location: "some location"
});

In jquery, if you don't like callbacks (which I highly hope you don't :) ) , you're in luck my friend. 在jquery中,如果您不喜欢回调(我非常希望您不喜欢:)),那么您很幸运,我的朋友。 There is a flag in the $.ajax() function that you can use to turn async off for that specific AJAX request. $ .ajax()函数中有一个标志,可用于关闭该特定AJAX请求的异步。 Below is code sample: 下面是代码示例:

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

var x = 1;
jQuery.ajax({
     url:    'http://example.com/catalog/create/',
     success: function(result) {
                x = 59;
              },
     async:   false
});

console.log(x);    //Should print 59 instead of 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM