This is not a jquery question.
I want to create a callback function...so users would use it like so:
myfunc.init('id',{option1: val, option2: val2}, function() {
//in here users can now call methods of myfunc
});
How do I do this within my code. OS once I know my script is ready I want to be able to somehow call this anonymous function.
Hope thi开发者_开发技巧s makes sense. I often don't.
You can write i t like this:
var myfunc.init(id, options, callbackFunction){
//do whatever you want with id & options
callbackFunction();
}
This will first run everything you want in your init function then run the function supplied as the callback parameter
Something like this:
var myfunc.init = function(id, options, func) {
// call func somewhere at some point
func();
};
Usually callbacks are used because you want to call it at some point at time which is unknown (asynch)... like for example when an AJAX request is complete:
var myfunc.init = function(id, options, func) {
myAjaxRequest("url.com", function() {
// call the func at this point in time
func();
});
};
精彩评论