2

Context

I update several sites with Ajax calls, one by one to conserve the server.

I made a recursive function that runs himself again when an Ajax call is completed.

Issue

The function is stopped after the first loop.

Any idea ?

Code

var updateSite = function (site) {
    if (site.status == 'waiting for update') {
        updateStatus(i, site, 'update in progress');

        $.get(site.url)
            .success(function () {
                updateStatus(i, site, 'updated');
            })
            .error(function () {
                updateStatus(i, site, 'not updated');
            })
            .complete(function () {
                updateSite(allSites[i++]);
            });
    }
};

var i = 0;

updateSite(allSites[i]);
5
  • @Amberlamps -- the next call is initiated by the .complete function Commented Aug 14, 2012 at 15:46
  • @LarryK: Thanks! I am obviously not used to jQuery :) Commented Aug 14, 2012 at 15:48
  • @GG -- be sure to test in multiple browsers, especially IE Commented Aug 14, 2012 at 15:48
  • @LarryK: This is an internal tool, if it works on chrome that's ok. :) Commented Aug 14, 2012 at 15:50
  • @GG: If you use allSites.shift() instead of allSites[i] you would not need i in the first place, but I guess you need i for further processing. Commented Aug 14, 2012 at 15:52

2 Answers 2

5

Change complete function as below:

function () {
     updateSite(allSites[++i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Any error messages?

Perhaps the var updateSite is not yet defined when the anonymous function you're using for complete is defined.

Try this:

var updateSite;
updateSite = function (site) {
    if (site.status == 'waiting for update') {
        updateStatus(i, site, 'update in progress');
.... // everything else the same...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.