0

I'm not sure how to approach this as I am fairly new to jQuery. I'm wanting to create a callback function within a custom function. Here's my example:

function doSomething() {
    var output = 'output here';
    // Do something here

    // This is where I want to create the callback function and pass output as a parameter
}

I want the callback function to be accessible by any number of scripts (e.g. more than one script can access this callback).

This function (doSomething) is not part of a plugin but rather part of another callback function itself. I've also created a var within the function and want to pass that through the callback function as well.

How can I do this?

2
  • So you're saying you don't have control of what is passed to doSomething? Depending on what framework your using that may not be true... can you share the code where you pass doSomething as a callback? Commented Mar 26, 2012 at 23:27
  • Yes, that is correct. I won't necessarily know what is being passed into doSomething, but it needs to be available for someone to come in and extend. Commented Mar 26, 2012 at 23:53

1 Answer 1

1

It is not very clear what you're asking. Creating a callback function is as simple as declaring a function and passing it as an argument to another function that is expecting a callback.

function doWork(fnCallback) {
    // do some work
    // when done with the work
    // call the callback
    var someVar = 1;
    fnCallback(someVar);
}

function myCallback(val) {
    alert("got my callback - val=" + val);
}

doWork(myCallback);

Or, using an anonymous function instead of a named function

doWork(function(val) {
    alert("got my callback - val=" + val);
});

If you really want to declare a function within a function, you can do that too:

function doSomething() {

    function myCallback(val) {
        alert("got my callback - val=" + val);
    }
    doWork(myCallback);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The first example looks like what I want to achieve, but can that callback function be called multiple times? So, say you load 3 scripts on the page and each script wants to hook into that callback using myCallback. Will there be issues with calling myCallback 3 times?
@Thomas - myCallback is just a regular function. You can call it as many times as you want. Functions are made to be called - that's their point in life. There is no difference between a function used as a callback and a regular function - they are the same thing.
Ok, that's what I wanted to know. I wasn't sure if the process was defining a new function or just calling it - that's where I was confused, because I know you can't define a function more than once, but you can certainly use it more than once. Thanks for clearing that up for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.