Raphael
Raphael

Reputation: 703

.animate() with two complete functions?

I wondered about if it's possible to add two or more complete functions to the .animate() event, like this:

.animate(properties [, duration] [, easing] [, complete][, complete][, complete])

because I don't want to have all the things that happened in one function. I want them separated in different complete functions!

Is this possible?

Upvotes: 1

Views: 93

Answers (4)

Guffa
Guffa

Reputation: 700382

As far as I can tell, the documentation only mentiones one callback function. It might work to use more than one, but that behavior could also change without notice, so you shouldn't use it anyway.

You can just put function calls in an anonymous function. Example:

 $('.moving').animate({ height: '20px' }, function() {
   callback1();
   callback2();
   callback3();
 });

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262979

You can compose your callback functions into a single one (note the use of apply() in order to call the functions in the same context as the original callback):

$("selector").animate(properties, duration, easing, function() {
    complete1.apply(this);
    complete2.apply(this);
    complete3.apply(this);
});

Alternatively, you can use the new $.Callbacks facility to group all your callbacks into a single object:

var animateCallbacks = $.Callbacks();
animateCallbacks.add([complete1, complete2, complete3]);

$("selector").animate(properties, duration, easing, function() {
    animateCallbacks.fireWith(this);
});

Upvotes: 2

Matteo Vinci
Matteo Vinci

Reputation: 208

No is not possible :-). The quickly way is call two function in the callback.

Upvotes: 0

Paul
Paul

Reputation: 141827

No, but you could easily pass in an anonymous function that calls your two (or more) functions like this:

.animate(properties, function(){
    callback1();
    callback2();
});

Where callback1 and callback2 are functions.

Upvotes: 4

Related Questions