admin管理员组

文章数量:1330389

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(){ }

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(){ }
Share edited Dec 15, 2011 at 7:00 Bhesh Gurung 51k23 gold badges95 silver badges143 bronze badges asked Dec 15, 2011 at 6:55 jQuerybeastjQuerybeast 14.5k39 gold badges119 silver badges198 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4
function main(callback) {
    // ... do your thing
    callback();
}

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

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 plete function like this:

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

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

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

}

本文标签: javascriptjQuery Create CallbackStack Overflow