Javascript:等待.apply()返回

时间:2017-07-03 09:49:34

标签: javascript jquery ajax handler apply

我想等待以执行调用执行AJAX操作的函数的.apply()执行的 return 。我的代码如下所示:

    //A function doing some AJAX stuff
    function f1(arg1, arg2) {
        $.ajax({
            type: "POST",
            url: "/some_url.php",
            data: "arg1="+arg1+"&arg2="+arg2,
            success: function(data) {
                if(data === "1")
                    return true;
                else
                    return false;
            }
        });
    }

    //Another function calling the first one
    function f2(arg1, arg2, other, things) {
        //What I would like to do
        if(f1.apply(null, [arg1, arg2]))
            doSomethingAwesome();

        //Continue code even if the AJAX isn't done yet
    }

if(f1.apply(null, args))并没有等待ajax完成,我明白它不应该这样做,但是是否有应用方法的处理程序?我做了一些测试,console.log()等待结果,所以我认为应该有办法。

我读了一些关于$.deferred和AJAX处理程序的内容,但是它们似乎总是在里面使用使用AJAX的函数。我还尝试.complete().done()但没有成功。

  

我真的想在另一个函数中使用AJAX,因为我会使用   它有很多不同的目的。

0 个答案:

没有答案