Xeon43
Xeon43

Reputation: 41

Do something after function is finished

I have a question, I think the answer will be simple, but I can't really find it ...

I have a function that creates content with ajax. After the function is finished I want to do something with the created content. To do that, I need to wait until all content is created, before I can do something with it.

What I prefer is something like this:

viewAllAccounts(function() {
   //do something
});

or

viewAllAccounts().queue(function() {
   // do something
});

But offcourse this is not going to work :)

I don't want to touch the viewAllAccounts function, because it is used multiple times in my app.

Is there a simple way to do something after a function is finished, not altering the function itself?

Thanks in advance!

Upvotes: 4

Views: 4161

Answers (3)

user142019
user142019

Reputation:

For if you do not want to waste hundreds of bytes and a bunch of milliseconds on jQuery*:

You can create a second function:

function viewAllAccountsWithFinishingFunction(func) {
  viewAllAccounts();
  return func();
}

Then call:

viewAllAccountsWithFinishingFunction(function() {
  //do something
});

* I do like jQuery, but using it for just one out of hundreds of features it has, I don't find it necessary.

Upvotes: 2

hvgotcodes
hvgotcodes

Reputation: 120308

when you invoke ajax calls with jquery, there are a bunch of handlers that fire when the ajax call completes. You should hook into those. Take a look-see at

http://api.jquery.com/jQuery.ajax/

particularly the 'success' and 'error' properties. just define functions for those and they will get called when the request completes.

Upvotes: 3

Related Questions