GG.
GG.

Reputation: 21854

Recursive function stopped after the first loop

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]);

Upvotes: 2

Views: 155

Answers (2)

IUnknown
IUnknown

Reputation: 22458

Change complete function as below:

function () {
     updateSite(allSites[++i]);
}

Upvotes: 5

Larry K
Larry K

Reputation: 49114

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...

Upvotes: 0

Related Questions