jquery callbacks being called multiple times

后端 未结 3 901
借酒劲吻你
借酒劲吻你 2020-12-15 06:47

When I use the fade/slide/animate functions in jQuery the callback gets called multiple times for each element the effect is applied to. This is by design of course. I jus

相关标签:
3条回答
  • 2020-12-15 07:01

    Try taking a look at the jQuery documentation for .promise()

    This should work:

    $('div').fadeOut(1000).promise().done(function() {
       alert('all done');
    });
    

    As usualy, the jQuery documentation on this is really vague. But I'm not sure I could do

    0 讨论(0)
  • 2020-12-15 07:16

    That would produce the alert when the fadeOut on the last element was called. That would not necessarily be the last fadeOut.

    var numDivs = $('div').length;
    $('div').fadeOut(1000, function() {
      if( --numDivs > 0 ) return;
      alert('this is the final fadeout to complete');
    });
    

    Check it out on JSFiddle

    0 讨论(0)
  • 2020-12-15 07:20
    $("div:not(:last)").fadeOut(1000);
    $("div:last").fadeOut(1000, function() {
        alert("Hey!");
    });
    
    0 讨论(0)
提交回复
热议问题