Answers:
您可以指定一个回调函数:
$(selector).fadeOut('slow', function() {
// will be called when the element finishes fading out
// if selector matches multiple elements it will be called once for each
});
在jQuery 1.6版本中,您可以使用.promise()方法。
$(selector).fadeOut('slow');
$(selector).promise().done(function(){
// will be called when all the animations on the queue finish
});
promise()或when()和done()您可以利用来自第三方方法的一些非常酷的行为,甚至是你自己。+1表示意义.promise()!
$(selector).fadeOut('slow').promise().done(function(){...});
您也可以使用$.when()等到promise完成:
var myEvent = function() {
$( selector ).fadeOut( 'fast' );
};
$.when( myEvent() ).done( function() {
console.log( 'Task finished.' );
} );
如果您执行的请求很可能失败,那么您甚至可以更进一步:
$.when( myEvent() )
.done( function( d ) {
console.log( d, 'Task done.' );
} )
.fail( function( err ) {
console.log( err, 'Task failed.' );
} )
// Runs always
.then( function( data, textStatus, jqXHR ) {
console.log( jqXHR.status, textStatus, 'Status 200/"OK"?' );
} );
$.when()不起作用,因为myEvent()它不会返回承诺,并且$.when()希望您通过一个或多个承诺来履行其职责。