Reputation: 14490
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
Reputation: 29498
function main(callback) {
// ... do your thing
callback();
}
main(function(){
alert('this is the callback speaking');
});
Upvotes: 4
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
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