Do something after the function finishes.

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

I have a function that creates content using ajax. At the end of the function, I want to do something with the created content. To do this, I need to wait until all the content has been created before I can do something with it.

I prefer something like this:

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

or

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

But to no avail, this is not going to work :)

I do not want to touch the viewAllAccounts function because it is used several times in my application.

Is there an easy way to do something after completing a function without changing the function itself?

Thanks in advance!

+4
source share
3 answers

when you call ajax calls with jquery, there are a bunch of handlers that fire when the ajax call ends. You have to catch on to them. Take a look at

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

in particular, the properties of success and error. just define functions for them and they will be called when the request is complete.

+3
source

If you don't want to spend 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 like jQuery, but, using it for only one hundred hundred functions, I do not consider it necessary.

+2
source

Source: https://habr.com/ru/post/1341826/


All Articles