admin管理员组

文章数量:1402923

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!

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!

Share Improve this question asked Mar 1, 2011 at 14:26 Xeon43Xeon43 411 silver badge2 bronze badges 1
  • adding a call to another function (that does soemthing) just before the return ; is called on viewAllAccounts ? – ypercubeᵀᴹ Commented Mar 1, 2011 at 14:29
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

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

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.

Check out jQuery deferred objects:

http://www.erichynds./jquery/using-deferreds-in-jquery/

http://api.jquery./category/deferred-object/

本文标签: javascriptDo something after function is finishedStack Overflow