1

I'm learning jquery and found callbacks.add() but could not understand properly in http://api.jquery.com/callbacks.add/

So can anyone give me a proper example so that I could understand about it.

Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68
  • 1
    What in particular do you not understand? It adds another function or multiple functions to the list. That's it really. If you are just learning jQuery, you don't need `$.Callbacks` **at all**. – Prinzhorn Sep 29 '13 at 10:21
  • I want learn jquery deeply so I was seeing for this but the link I provided is not describing about it so much as in other description. – Navin Rauniyar Sep 29 '13 at 10:23
  • `$.Callbacks` is basically not jQuery related. It's just a utility function under the `jQuery` namespace. – Prinzhorn Sep 29 '13 at 10:35

3 Answers3

0

Simply put, callbacks.fire() will call a function (or series of functions) in a row. Callbacks.add(function) adds another callback to the chain. Here is a page that describes how callbacks work in javascript:

0

They are a means of calling already defined functions. So, imagine you have the following function:

function hello(value) {
    console.log(value);
}

Now, in order to call that function, you can use a Callback like so:

var callbacks = $.Callbacks();
callbacks.add(hello);

But, when you fire that function, you have to specify the argument as defined in the hello function. So hence, it would be:

callbacks.fire("Hello all");

Another example/use of callback functions is when you have animations or effects and you want an effect to occur after another effect has completed. For example, you have a paragraph element and you want to hide it. But after the paragraph is hidden, you want an alert stating that it's hidden. So here you would use a callback function.

$("button").click(function(){
    $("p").hide("slow",function(){
        alert("The paragraph is now hidden");  // call-back function after p is hidden.
    });
});
blackpanther
  • 10,998
  • 11
  • 48
  • 78
-1

I think this might be helpful for you

When would I use JQuery.Callbacks?

http://api.jquery.com/jQuery.Callbacks/

Community
  • 1
  • 1
SarathSprakash
  • 4,614
  • 2
  • 19
  • 35