1

I have an asynchronous problem and for solve it I think I must use a callback. I want to execute some code after the code that is inside of the click() function finishes. How can I do that?

$("#mybutton").click(function() {
    $.ajax({
      type: 'GET',
      data: {
        accion: "get_option_child",
        id: id
      },
      url: 'webservices.php',
      success: function(data) {
        var json = JSON.parse(data);
        // some code
      }
  });
});
1
  • 1
    Call your function from inside the function you provide to the success parameter Commented Jan 5, 2017 at 19:15

1 Answer 1

3

Just add a call to your desired function at the end of the success handler for ajax:

$("#agregar_opcion").click(function() {
  // ...
  $.ajax({
    // ...
    success: function(data) {
      // ...
      functionThatRunsAfterAjaxSuccess();
    }
  });
});

function functionThatRunsAfterAjaxSuccess() {
  // gets called once the ajax call happening on click is successful
}
Sign up to request clarification or add additional context in comments.

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.