2
$(function(){

    var dfd = $.when( $('div').eq(0).fadeOut(1000) );

    // ADD NOW NEW FUNCTION DO dfd

    dfd.then(function(){
        alert();
    });

});

如果它正在运行但尚未解决,是否可以向 dfd 延迟对象添加另一个函数?

4

3 回答 3

1

It's a bit unclear, but I think that you want to check whether the deferred is resolved and add another callback if it's not:

http://jsfiddle.net/RT23y/1/

Essentially you can just check

if (dfd.state() == 'resolved')

to see whether another callback can be added.

于 2012-12-27T18:31:08.747 回答
0

在标准的 jQuery 方法链中,我们期望.fadeOut()返回 jQuery,但是在表达式中......

$.when( $('div').eq(0).fadeOut(1000) )...;

...通过一个内部 jQuery 魔术,传递给的参数实际上$.when()是一个从. 明确地说,我们会写...fx$('div').eq(0)

$.when( $('div').eq(0).fadeOut(1000).promise('fx') )...;

实际上没有理由使用$.when(),除非你需要做一些事情来响应两个或更多的承诺。因此我们可以写...

var promise = $('div').eq(0).fadeOut(1000).promise();

...给出可以以多种方式附加任意数量的“完成”回调的承诺。

这是可能的示例:

var promise = $('div').eq(0).fadeOut(1000).promise();

promise.then(function() {
    alert('done 1');
});

promise.done(function() {
    alert('done 2');
});

promise.then(function() {
    alert('done 3');
}).always(function() {
    alert('done 4');
});

演示

于 2012-12-27T22:19:09.787 回答
-1

您可以使用.promise()

$('div').eq(0).promise().done(function(){
    alert();
});
于 2012-12-27T17:20:17.987 回答