6

I understand that you can pass a function to slideUp/slideDown that will run once the transition has completed:

$('li').slideUp({
    complete: function(){
        alert('done');
    }
});

With this HTML an alert is shown 3 times.

<ul>
    <li>[content here]</li>
    <li>[content here]</li>
    <li>[content here]</li>
</ul>

Is there some way to have the complete function fire only once, when the last list item has completed the slideUp? (fiddle)

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228

2 Answers2

17

You can "redo" your selector and check if this is the last :

$('li').slideUp({
    complete: function(){
        if($('li:last').is(this)){
            alert('done');
        }
    }
});

Fiddle : http://jsfiddle.net/5NQsU/

Caching your selector would be even better :

var $li = $('li');
$li.slideUp({
    complete: function(){
        if($li.last().is(this)){
            alert('done');
        }
    }
});
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • `.is(this)`...damnit, I was trying to remember this, +1 – tymeJV Jul 17 '13 at 20:09
  • Brilliant, thanks, will accept as soon as the system allows me. I suppose this would work just the same with `:first`, right? Since they all complete at the same time? I guess I could even have used a `isCompleted` flag or something now that I think of it... – Wesley Murch Jul 17 '13 at 20:09
  • @WesleyMurch It should work as well :) – Karl-André Gagnon Jul 17 '13 at 20:11
2

http://jsfiddle.net/Ultimate/KrW5a/

var total = $('ul').size();
var i = 1;
$('li').slideUp({
    complete: function () {
        if (i === total) alert('done');
        i += 1;
    }
});
Shawn31313
  • 5,978
  • 4
  • 38
  • 80
PaulBGD
  • 2,018
  • 5
  • 22
  • 30