Brian
Brian

Reputation: 315

How to: callback function that takes ajax get array as a parameter

How would I go about writing a function that takes a function as a parameter and runs the callback function with an array pulled from the server as its parameter. So if I ran something like:

getArray(display)

getArray would retrieve an array from the server, then display would write that array into the html.

Upvotes: 0

Views: 42

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Like so:

function getArray(callback) {
    $.ajax({
        ..//options
        success: function(data) {
            callback(data); 
        }
    });
}

function display(array) {
    console.log(array);
}

//call it!
getArray(display);

Upvotes: 1

Related Questions