0

i want to start a function after a successful ajax call. What i do now is this

function ajaxCall(url, types, next) {
    $.ajax({
        url: url,
        type: 'POST',
        beforeSend: function () {

            $("#plaatsMelding").append("<div id=\"" + types + "verwerkt\"><div class=\"progress-label\">" + types + " verwerken...</div></div>");
        },
        success: function (data) {
            $('#plaatsMelding').empty();
            $("#plaatsMelding").append("<div id=\"" + types + "verwerkt\"><div class=\"progress-label\">" + data + "</div></div>");
            //run next script
            if (next.length) {
                alert(next);
                next(); //this won't work because it will search for the next() function
            }

        }
    });
}

i start this function with this function:

function saveRace() {
    ajaxCall("verwerkRace.php", "race", "savePlayers");
}

so when that function is ready i want it to run the function "savePlayers". How can i do this?

function savePlayers() {
    ajaxCall("verwerkPlayers.php", "players");
}
4

3 回答 3

3

Don't pass a string, pass a function:

function saveRace() {
    ajaxCall("verwerkRace.php", "race", savePlayers);
}

Then in your success you can do this:

if (next) {
    next(); 
}
于 2013-03-30T20:59:01.980 回答
2

Just pass in the function itself:

function saveRace() {
  ajaxCall("verwerkRace.php", "race", savePlayers);
}

The JavaScript knows nothing of turning your string into a function to be called.

于 2013-03-30T20:59:26.607 回答
0

As suggested, pass a function instead of a string

function saveRace() {
    ajaxCall("verwerkRace.php", "race", savePlayers);
}

In success you can then check to ensure you're passing a function, and then fire it:

 if (next && typeof(next) === 'function') {
    next(); 
 } else {
    console.error('Error firing the callback!');
 }
于 2013-03-30T21:01:56.873 回答