0

What i'm trying to do might sound simple.

I just need to make a callback with another function Here is an example using jQuery.

(function ( $ ) {
    "use strict";
    var colors;

    colors = function(col){
        return col;
    };

    $(document).ready(function(){
        colors(function(){alert('Orange');});
    });
})(jQuery);

So when the document be ready, i need to send an alert that says 'Orange' but i want to do it by returning a function.

If you know another way to do it please let me know.

1
  • Strictly speaking, what you're asking to do is not possible. The way you make an alert popup happen is by calling the alert() function. Commented Sep 23, 2015 at 21:56

1 Answer 1

3

To call a function, you have to put () after it. Since colors() returns the function, put () after the call to colors:

$(document).ready(function(){
    colors(function(){alert('Orange');})();
});

More generally, the way you deal with callbacks is something like:

callback = getCallback(args);
callback();
Sign up to request clarification or add additional context in comments.

3 Comments

This may very well be what the OP is (mysteriously) trying to do :)
I suspect it's a simplification -- he may be calling a function that returns a function from a table of some kind.
i'm creating a plugin, for animations, so when de animation ends, if the user want, can make a callback

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.