jQuerybeast
jQuerybeast

Reputation: 14490

jQuery Create Callback

I'd like to create a callback on a simple function.

I have this function which is called on button click:

function main(){ };

So I'd like main(), when its done to call this:

 function test(){ }

Upvotes: 3

Views: 3729

Answers (3)

Michael Robinson
Michael Robinson

Reputation: 29498

function main(callback) {
    // ... do your thing
    callback();
}

main(function(){
    alert('this is the callback speaking');
});

Upvotes: 4

artwl
artwl

Reputation: 3582

if the main() function not use ajax,you can use:

function main(callback) {
    // ... do your thing
    callback();
}

function test(){}

eg:
<input type="button" onclick="main(test);"/>

if the main() function use ajax,you can call test() in complete function like this:

function main(callback){
    $.ajax({
        ...
        complete: function(XMLHttpRequest, textStatus){
            callback(); 
        },
        ...
    });
}

function test(){
    ...
}
eg:
<input type="button" onclick="main(test);" value="test"/>

Upvotes: 3

sampathpremarathna
sampathpremarathna

Reputation: 4064

As i understood its simple,Sorry if i am not in the point.

function main() {
// ... do your thing
test();
}


function test() {
// ... do your thing in test

}

Upvotes: 1

Related Questions