0

I have a series of jQuery functions to execute in a block. When they ALL are done, I need another function to be called. It seems a callback is the best way to do this. Here is my code.

 function flipPage (x, callback) {

    $("#view-1").animate({
    width: x,
    }, 500);
  $("#view-2").animate({
    width: browserWidth - x,
    }, 500);
  $("#iframe-2").animate({
    left:   -x,
    }, 500);
  $("#sweeper").animate({
    left:   x,
    }, 500);

  callback(function() {
    alert("I'm Done.");
    //do the next thing
  }) ;
}
5
  • It does not work. I get Uncaught TypeError: undefined is not a function Commented Jan 27, 2014 at 19:54
  • How are you calling flipPage? Commented Jan 27, 2014 at 19:55
  • 1
    Why are you passing all those empty functions to .animate()? Commented Jan 27, 2014 at 19:56
  • Good question on the empty functions. My bad. Removed. I'm calling flipPage as flipPage(20); Commented Jan 27, 2014 at 20:00
  • @JonPaulBerti: There's your problem! You aren't passing in anything for callback, so it's undefined... which is not a function. Commented Jan 27, 2014 at 20:01

2 Answers 2

2

You could do something like this:

function flipPage (x, callback) {
      var animationHelper = {
        actionCounter: 0,
        actionCount: 4,
        callbackHandler: function() {
            this.actionCounter++;
            if(this.actionCounter == this.actionCount)
                this.callbackAllFinished();
        },
        callbackAllFinished: null
      };

      $("#view-1").animate({
        width: x }, 500, animationHelper.callbackHandler);

      $("#view-2").animate({
        width: browserWidth - x }, 500, animationHelper.callbackHandler);

      $("#iframe-2").animate({
        left: -x }, 500, animationHelper.callbackHandler);

      $("#sweeper").animate({
        left: x }, 500, animationHelper.callbackHandler);

      animationHelper.callbackAllFinished = function() {
        alert("I'm Done.");
        //do the next thing
      };
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks sjkm. Can anyone offer a more simple example solution?
0

Essentially the same as sjkm's, but simplified.

function flipPage (x, callback) {
    var counter = 0;

    $("#view-1").animate({
        width: x,
    }, 500, finished);

    $("#view-2").animate({
        width: browserWidth - x,
    }, 500, finished);

    $("#iframe-2").animate({
        left:   -x,
    }, 500, finished);

    $("#sweeper").animate({
        left:   x,
    }, 500, finished);

    function finished() {
        counter++;      
        if (counter >= 4) {
            alert("I'm Done.");
            //do the next thing if callback exists
            callback && callback.call();          
        }
    };
}

jsfiddle

Comments

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.