Androme
Androme

Reputation: 2449

JQuery Remove last element, multi calls

Hello i have the following JQuery code that will remove the last element in a container. THe problem is that my remove is animated, and this method will for example be called 10 times in a row, then will only 2 or 3 elements be remove, because the next wall will catch the object that is already being removed, but is currently fading out.

        var last = self.options.FollowsContainer.children().last();
        self.usersInFollowsList--;
        if (last != null) {
            last.fadeOut(function () {
                $(this).remove();
            });
        }

I have tryed the following, but it did not work

    var last = self.options.FollowsContainer.find(":not(.removing):last");
    self.usersInFollowsList--;
    if (last != null) {
        last.addClass("removing");
        last.fadeOut(function () {
            $(this).remove();
        });
    }

Upvotes: 0

Views: 92

Answers (2)

Utopik
Utopik

Reputation: 3783

using the jQuery selector :animated

self.options.FollowsContainer.children(":not(:animated):last").fadeOut(function () {
    this.remove();
});

It gets the last not animated child and fade it out

Upvotes: 1

Smern
Smern

Reputation: 19066

Here is my solution, modified to easily fit in a jsfiddle: http://jsfiddle.net/Lb9qA/

var numBeingRemoved = 0;
$("#remove").on("click", function () {
    numBeingRemoved++;
    $("#foos").children(":nth-last-child(" + numBeingRemoved + ")").fadeOut(function () {
        this.remove();
        numBeingRemoved--;
    });
});

Upvotes: 1

Related Questions