0

I am having difficulty calling a function after a jQuery AJAX call has finished loading

function to_be_executed_last {
    alert("The Call Is Complete");
    //some other stuff
}



$("a.jaxable_link").click(function(){
   $.post($(this).attr('href'), function(data) {

        $('#container').html(data);
 //Execute the function to_be_executed_last here AFTER data has finished loading
  })
})

How can I do this? Thanks

2 Answers 2

1

You just need to fix the signature (missing parenthesis) and call it, like this:

function to_be_executed_last() {
  alert("The Call Is Complete");
  //some other stuff
}

$("a.jaxable_link").click(function(){
  $.post($(this).attr('href'), function(data) {
    $('#container').html(data);
    to_be_executed_last();
  });
});
Sign up to request clarification or add additional context in comments.

5 Comments

10 seconds. 10. Seconds. ;-) (Ahead of me, but yours was more complete.)
That often calls the function BEFORE the call is complete
@RisingSun: It never calls the function before the call is complete, not when done properly (as Nick has above). He's called it from within the success callback, which by definition happens when the call is complete.
@RisingSun - make sure you have it inside the callback for $.post() not after $.post() and it'll run after the .html() call.
That was the problem! Thanks.
0

Simply call your function. The success callback of all jQuery ajax functions is invoked when the response has been received.

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.