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.
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.
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:
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.
});
});
I think this might be helpful for you