简体   繁体   English

如何使用$ .when作为回调函数?

[英]How to use $.when for a callback function?

I want to say when this function close() is finished run this function init(). 我想说的是,当函数close()完成时,请运行该函数init()。 But it's not working for me. 但这对我不起作用。

$.when(close(toolTip)).done(init(toolTip, anchor));

I am not using the $.when for anything ajax related, just trying to make sure close() is finished before I call init(), and no I can't stick init() at the end of close(). 我没有将$ .when用于与ajax相关的任何事情,只是试图确保在调用init()之前完成close(),而且不,我不能在init()的结尾粘贴init()。 Any ideas? 有任何想法吗?

ok here is close() 好的,这里是close()

var close = function (toolTip) {
    toolTip.fadeOut('fast', function (e) {
        if (typeof e !== 'undefined') {
            //Re-set values applied when initted
            var toolTipBd = toolTip.find('.bd:first');
            toolTip.css('width', '');
            toolTipBd.css('max-height', '');
            toolTip.css('max-height', '');
            toolTipBd.css('overflowY', '');
        }
    });
};

No where in close() can it call init(). 在close()中的任何地方都无法调用init()。

Your close() implementation should be like this: 您的close()实现应如下所示:

var close = function (toolTip) {
    var d = $.Deferred();

    toolTip.fadeOut('fast', function (e) {
        if (typeof e !== 'undefined') {
            //Re-set values applied when initted
            var toolTipBd = toolTip.find('.bd:first');
            toolTip.css('width', '');
            toolTipBd.css('max-height', '');
            toolTip.css('max-height', '');
            toolTipBd.css('overflowY', '');
        }

        d.resolve();
    });

    return d.promise();
};

$.when works with Deferred 's . $.when作品与Deferred It returns a new Deferred which will resolve when all the Deferred 's you provided resolve. 它返回一个新的Deferred当所有这将解决Deferred的你提供的决心。

As close() doesn't seem to be returning a Promise, when will resolve straight away (per the docs for when() . 由于close()似乎没有返回Promise,因此when可以立即解决(根据when()文档when()

However, if close() is synchronous, you don't need when() at all. 但是,如果close()是同步的,则根本不需要when() If it is asynchronous, you need to return a Promise , and resolve it when your animation or whatever has completed; 如果它异步的,则需要返回Promise ,并在动画或其他完成时解决它;

function close(what) {
    var promise = jQuery.Deferred();

    what.fadeOut('slow', function () {
        promise.resolve();
    });

    return promise.promise();
}

... but you still don't need $.when as only 1 promise is involved. ...但是$.when只涉及1个诺言时,您仍然不需要$.when .。 $.when is only useful when multiple promises are at play. $.when仅在多个诺言同时起作用时才有用。

close(toolTip).done(function () {
    init(toolTip, anchor);
});

Note also that done(init(tooltip, anchor)) will call init immediately, and pass the result of that function invocation to done() ; 还要注意, done(init(tooltip, anchor))将立即调用init ,并将该函数调用的结果传递给done() instead, you need to pass a function to done. 相反,您需要传递一个函数来完成。 As init needs parameters, we've fixed this by introducing an anonymous function. 由于init需要参数,因此我们通过引入匿名函数来解决此问题。 If init didn't need any parameters, it'd have been as simple as: 如果init不需要任何参数,它就很简单:

close(toolTip).done(init);

Simply return toolTip: 只需返回工具提示:

return toolTip.fadeOut(...

using the callback to resolve a deferred object can result in odd results if there are more than one elements selected for whatever reason. 如果出于任何原因选择了多个元素,则使用回调来解析延迟的对象可能会导致奇怪的结果。

This works because jQuery objects have a .promise method that when called, return a promise object that resolves when all active animations are completed. 这是.promise ,因为jQuery对象具有.promise方法,该方法在被调用时返回一个promise对象,该对象将在所有活动动画完成时进行解析。 $.when calls .promise on all passed in arguments. $.when .promise在所有传入的参数上调用.promise时。

You'll also need to call init differently, for example, 您还需要以不同的方式调用init,例如,

$.when(close(toolTip)).done(function(){
    init(toolTip, anchor);
});

And, as pointed out by others, you could then shorten that to 而且,正如其他人所指出的,您可以将其缩短为

close(toolTip).promise().done(function(){
    init(toolTip, anchor);
});

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

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