简体   繁体   English

jquery自定义延迟函数

[英]jquery custom deferred functions

I have three functions i'm trying to run, the first two are doing some async stuff that need data for the third to use. 我有三个函数我正在尝试运行,前两个正在做一些异步的东西,需要第三个使用的数据。 I want the third function to fire only when 1 and 2 are both done. 我希望只有当1和2都完成时才会触发第三个函数。 this is the general structure but the final function is firing before 1 and 2 finish. 这是一般结构,但最终功能是在1和2结束前开始。

function run() {
    var data1 = {};
    var data2 = {};

    $.when(first(), second()).done(constructData());

    function first() {
        var d = new $.Deferred();

        //do a bunch of stuff async
        data1 = {};

        d.resolve();
    }
    function second() {


        var d = new $.Deferred();

        //do a bunch of stuff async
        data2 = {};
        d.resolve();
    }
    function constructData() {
        //do stuff with data1 and data2
    }

}

Answer was to not call construct data immediately 答案是不立即调用构造数据

 $.when(first(), second()).done(constructData);

You should return promise object. 你应该返回promise对象。 You also have an error in this line: 此行中也有错误:

$.when(first(), second()).done(constructData());

it should be 它应该是

$.when(first(), second()).done(constructData); // don't call constructData immediately

So all together it could be: 所以它们可能是:

function run() {
    var data1 = {};
    var data2 = {};

    $.when(first(), second()).done(constructData);

    function first() {
        return $.Deferred(function() { // <-- see returning Deferred object
            var self = this;

            setTimeout(function() {   // <-- example of some async operation
                data1 = {func: 'first', data: true};
                self.resolve();       // <-- call resolve method once async is done
            }, 2000);
        });
    }
    function second() {
        return $.Deferred(function() {
            var self = this;
            setTimeout(function() {
                data2 = {func: 'second', data: true};
                self.resolve();
            }, 3000);
        });
    }
    function constructData() {
        //do stuff with data1 and data2
        console.log(data1, data2);
    }
}

http://jsfiddle.net/FwXZC/ http://jsfiddle.net/FwXZC/

I think you should have first() and second() return a promise: return d.promise(); 我认为你应该让first()second()返回一个promise: return d.promise(); . From the docs : 来自文档

If a single argument is passed to jQuery.when and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. 如果将单个参数传递给jQuery.when并且它不是Deferred或Promise,则将其视为已解决的Deferred,并且将立即执行任何附加的doneCallbacks。

I suspect this might be why the when call is calling constructData too soon. 我怀疑这可能是为什么when调用过早调用constructData原因。

It's hard to tell from you code, but be sure you are calling d.resolve() after the async operations have completed. 从您的代码中很难说,但确保在异步操作完成后调用d.resolve()

You might find that a more natural approach to explicitly setting data1 and data2 is instead to use the data that is supplied when resolve is called. 您可能会发现,显式设置data1data2的更自然的方法是使用调用resolve时提供的数据。 This would mean that your when call would look something like this: 这意味着,你when调用看起来是这样的:

$.when(first(), second()).done(function(result1, result2) {
    data1 = result1[0];
    data2 = result2[0];

    constructData();
});

Note that the exact format of results supplied to the done method depends on the nature of the deferred objects. 请注意,提供给done方法的结果的确切格式取决于延迟对象的性质。 If the promises are returned from a call to $.ajax , the results should be of the form [data, statusText, jqXhrObject] . 如果从$.ajax的调用返回promise,则结果应为[data, statusText, jqXhrObject]

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

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