为了账号安全,请及时绑定邮箱和手机立即绑定

如何让jQuery等待效果完成?

如何让jQuery等待效果完成?

泛舟湖上清波郎朗 2019-11-27 14:11:56
我敢肯定,前几天我读到了这件事,但似乎找不到任何地方。我有一个fadeOut()事件,之后我删除了元素,但是jQuery在有机会完成淡出之前删除了该元素。如何让jQuery等待元素淡出,然后将其删除?
查看完整描述

3 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

您可以指定一个回调函数:


$(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

});



查看完整回答
反对 回复 2019-11-27
?
交互式爱情

TA贡献1712条经验 获得超3个赞

在jQuery 1.6版本中,您可以使用.promise()方法。


$(selector).fadeOut('slow');

$(selector).promise().done(function(){

    // will be called when all the animations on the queue finish

});


查看完整回答
反对 回复 2019-11-27
?
莫回无

TA贡献1865条经验 获得超7个赞

您也可以使用$.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"?' );

    } );


查看完整回答
反对 回复 2019-11-27
  • 3 回答
  • 0 关注
  • 576 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信