javascript jquery自定义延迟函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15018931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:10:54  来源:igfitidea点击:

jquery custom deferred functions

javascriptjqueryjquery-deferreddeferred

提问by Brian

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. this is the general structure but the final function is firing before 1 and 2 finish.

我有三个我正在尝试运行的函数,前两个正在做一些需要数据供第三个使用的异步操作。我希望第三个函数仅在 1 和 2 都完成时触发。这是一般结构,但最终功能是在 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);

回答by dfsq

You should return promise object. 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/

回答by nick_w

I think you should have first()and second()return a promise: return d.promise();. From the docs:

我认为你应该有first()second()返回一个承诺:return d.promise();。从文档

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 whencall is calling constructDatatoo 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 data1and data2is instead to use the data that is supplied when resolveis called. This would mean that your whencall would look something like this:

您可能会发现一个更自然的方式来明确设置data1data2是不是使用时所提供的数据resolve被调用。这意味着您的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 donemethod depends on the nature of the deferred objects. If the promises are returned from a call to $.ajax, the results should be of the form [data, statusText, jqXhrObject].

请注意,提供给该done方法的结果的确切格式取决于延迟对象的性质。如果 Promise 是从对 的调用返回的$.ajax,则结果应为[data, statusText, jqXhrObject].