ajmajmajma
ajmajmajma

Reputation: 14216

jquery run function after first one is complete

I am using a function that does an ajax call (it's rather big) because I have to use it in multiple places and it's a rather large ajax call. I manually call another ajax call afterwards to update some things on the page accordingly. The problem I am having is the second call is running before the first one is complete.

I am looking to do something like function().done( run other call in here). I dont want the second function to run until the first one is done and has updated the database because the second function should pull the new information.

Is something like this possible in jquery?

Upvotes: 0

Views: 1084

Answers (2)

Fseee
Fseee

Reputation: 2631

I suggest to call your function on ajax success:

$.ajax({
  url: "yourUrl",
  //...o
  success: callNextFunction(),
  error: rollBackFunction()
});

function callNextFunction(){
//your code here
}

Upvotes: 0

Loren Posen
Loren Posen

Reputation: 235

Exactly as you said, you can call .done() on an Ajax call!

Below is example code taken from the jQuery documentation:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

Upvotes: 3

Related Questions