redconservatory
redconservatory

Reputation: 21934

jQuery how to "chain" a conditional/function

I have the following block of code:

if (current <= total) {

            $next.css({opacity: 0.0})
            .addClass("active")
            .animate({opacity: 1.0}, 1000, function() {
                current -= 1;
                if (current == 1)
                    {
                        jQuery("#next").animate({opacity: 0}, 500); 
                        next_isShowing = false;
                    }
                if (!prev_isShowing) 
                    {
                        jQuery("#prev").animate({opacity: 1.0}, 500); 
                    }


            });

The only thing is, I actually want to move this block of code before the ".animate":

    current -= 1;
if (current == 1)
            {
                jQuery("#next").animate({opacity: 0}, 500); 
                next_isShowing = false;
            }

Is there a way to take this block of text and "chain" it into my current chain?

Upvotes: 4

Views: 1869

Answers (2)

redconservatory
redconservatory

Reputation: 21934

In this particular case since the decrement and conditional acted on another object, I could just move it outside of the chain:

if (current <= total) {

            $next.css({opacity: 0.0})
            .addClass("active")
            .animate({opacity: 1.0}, 1000, function() {
                if (!prev_isShowing) 
                    {
                        jQuery("#prev").animate({opacity: 1.0}, 500); 
                    }
            });

           current -= 1;
           if (current == 1)
              {
                jQuery("#next").animate({opacity: 0}, 500); 
                next_isShowing = false;
              }
}

But I found this plugin in case anyone needs to chain a conditional:

http://benalman.com/code/javascript/jquery/jquery.ba-cond.js

Works like:

Upvotes: 0

SLaks
SLaks

Reputation: 887797

It sounds like you're trying to write

$(...)
    .filter(function() { return current === 1; })
    .animate(...)
    .end()
    .thingy(...)

Upvotes: 1

Related Questions