0

I have this simple part of code where callback function is not working:

$(document).ready(function() { 
    $(".target:nth-child(3)").addClass("animated shake",function(){
      $("body").addClass("animated hinge")
    });
  });

first function works just fine ( elements are shaking) but nothing happens after that.

I'm also using this css library: https://github.com/daneden/animate.css/blob/master/animate.css

2
  • 1
    According to the documentation, .addClass() doesn't accept a callback function as a second argument. (It does optionally accept one as its only argument, but not for the purpose you are trying to use it for.) Commented May 19, 2017 at 7:45
  • Only the jQueryUI version of the addClass() function only accepts a callback parameter - not the plain jQuery version: api.jqueryui.com/addClass Commented May 19, 2017 at 7:46

1 Answer 1

1

You don't need a callback function in this case as the Jquery AddClass is not a asnychronous method i.e. the next statement will be executed only once addClass is executed.

This should simply work in your case:

$(".target:nth-child(3)").addClass("animated shake");
  $("body").addClass("animated hinge")
Sign up to request clarification or add additional context in comments.

9 Comments

So if the first CSS class has some associated (non-JS) animation, will the second one be added before or after that animation completes?
Implicitly, it doesn't know about non-js animations. You'll have to handle that thing yourself using setTimeout function.
@DJYadav thanks for your answer. I've just tested your solution and it looks like both functions are executed simultaneously, not one after another ( which is what I'm aiming to achieve)
@Pawel - It would help if you edited your question to more clearly explain that adding the first class causes CSS-based animation and that you want to wait for that to complete before adding the other class. (If indeed that is what you are trying to do.)
For this kind of thing, yes the only possibility is setTimeout.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.