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(){ }
function main(callback) {
// ... do your thing
callback();
}
main(function(){
alert('this is the callback speaking');
});
test function as the callback parameter of main. Updated my answer with more detailsif 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"/>
API.loadAndWait('snippet', '5', function() {main(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
}
test() after(!) you did whatever you have to do in main(). We don't know your code so we cannot guess where in your code you'll have to place the call to test(). Show us more code an we can help you