javascript - Trigger a function only after the completion of multiple AJAX requests -


I have found a special function that I have to run once, and only after completing many AJAX requests.

My current solution looks like this:

  function doWork () {// This is a task to run once after all requests} // Some tracking / Count variable var ajaxDoneCounter = 0; Var numOfAjaxRequests = 5; Var workDone = false; Function doWorkTrigger () {ajaxDoneCounter ++; If (! Done & amp; AJAX Door Quarter> = numOfAjaxRequests!) {WorkDone = true; do your work(); }} // .. // and many Ajax requests (something hidden in the function, etc.) / they look something like this: $ .ajax ({url: "http://www.example.com" , Data type: "Jason", success: function (data) {// variables in data to load, doWorkTrigger ();}});  

One of the obvious drawbacks above is that any AJAX call that is not successful, AJAXDoneCount and therefore doWork () might not increase Never call me error any $ I can get around Ajax using callback, so I do not have much anxiety.

What do I need to know whether the above is safe and / or good practice?
Is there any trick I missed, or anything that could work better?

update: Since jQuery 1.5, provide cleaner solutions. .


I will use whenever an Ajax call is completed (success or error):

  var numOfAjaxRequests = 5; $ (Document) .jaxComplete (function () {numOfAjaxRequests-- if (! NumOfAjaxRequests) {doWork ();}});  

Then you do not need to edit each Ajax request.

You can also use to inform Ajax requests instead of strict coding (but I'm not sure that it actually works, maybe you run Situation):

  var numOfAjaxRequests = 0; $ (Document) .jaxSend (function () {numOfAjaxRequests ++;});  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -