0

I made a very simple plugin for my personal use that allows animated rotations in jQuery.

And I added callback functions.

If the rewind parameter is true, the rotation is undone and then, a callback.

If it's false, the callback is immediate after the rotation.

I had to do this because, for some reason, the .promise().done(function(){ ... }) is called before complete: of animate()

So I had to skirt this.

Now the problem is that if I target multiple elements, the callback is called for each element. 'cause I use an each function I presume. But I want only one callback for the whole animation.

Here is the fiddle

How to do this please? Thx !

PS: I saw a similar question here : jQuery $.animate() multiple elements but only fire callback once but not applying because of the .promise().done() issue. And I don't want to add a variable, I want to understand ^^

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
Ann MB
  • 146
  • 1
  • 13

1 Answers1

1

You could try using the index of each and only call the callback if it is the first element

Something like:

return this.each(function(i) {
   ....
   ....
   complete: function() {
        i==0 && a.callback &&  a.callback()
  }
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Fastest and most efficient answer ever :) Thanks ! It works perfectly – Ann MB Dec 13 '17 at 00:01
  • I saw another place you call the callback. Not quite sure how it works there. Didn't look at it very long – charlietfl Dec 13 '17 at 00:02
  • I call it at two places, one if there is no rewind option, one if there is the rewind option. To avoid callback between the forward and backward animation. – Ann MB Dec 13 '17 at 00:06