tags:

views:

27

answers:

2

Hi,

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 this makes sense. I often don't.

+2  A: 

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();
    });
};
Luca Matteis
THanks, I'm waiting for the youtube api to tell me the player is ready. When it is then I want to allow the user to cue new videos/shift playback head etc.
elduderino
+2  A: 

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

Charlie boy
Of course, I was over-complicating it in my head
elduderino