I would like to create a custom callback function for a custom function and pass callback as a parameter.
function customFunction(a, b, callback) {
// Some code
}
customFunction("val1", "val2", function(){
//Code to execute after callback
});
You're almost there...
function customFunction(a, b, callback) {
// Some code
if (typeof callback === 'function') {
callback();
}
}
customFunction("val1", "val2", function(){
//Code to execute after callback
});
if (typeof callback === 'function') { callback(); } in case you have no 3rd parameter.The solution of Coulson is good. To show it more clearly we can add the timeout function as follow:
function customFunction(a, b, callback) {
setTimeout(function(){
console.log("original function");
callback();
},1000);
}
$(document).ready(function() {
$('#test').click(function() {
customFunction("val1", "val2",function(){
console.log("callback function");
});
});
});
Following is the link for check - https://jsfiddle.net/y0ch9exw/