递归jQuery Ajax

I want my function to process the results, then call itself from the done callback.

function geocode(id) {
    console.log(id + ' geocode');
    $.ajax({
        url: '/customers/' + id + '/geocode',
        dataType: 'json',
        data: {
            id: id,
        }
    }).done(function() {
        var newID = id++;
        console.log(id + ' done.');
        geocode(newID);
    });
}

This isn't incrementing though, say if I start it at 1, it just loops on 2 forever. I know it's a pain returning data from ajax, but I've never fully got my head round the nuances

You need to increment the variable before you set it to the value of newId:

function geocode(id) {
    console.log(id + ' geocode');
    $.ajax({
        url: '/customers/' + id + '/geocode',
        dataType: 'json',
        data: {
            id: id,
        }
    }).done(function() {
        console.log(id + ' done.'); // show this before incrementing
        var newID = ++id; // note the operator order here
        geocode(newID);
    });
}

Working example

Your id will be 1 on the first iteration, and 1 on the second (and third, fourth etc.) iteration as well as you never update it's value.

Try this:

function geocode(id) {
    console.log(id + ' geocode');
    $.ajax({
        url: '/customers/' + id + '/geocode',
        dataType: 'json',
        data: {
            id: id,
        }
    }).done(function() {
        id = id++;
        console.log(id + ' done.');
        geocode(id);
    });
}