Saturday, 26 March 2016

What is the use of jQuery Callback functions ?


jQuery Callback functions

A callback function is executed after the current effect is 100% finished.

JavaScript statements are executed line by line.
However, with effects, the next line of code can be run even though the effect is not finished. 
This can create errors.

To prevent this, you can create a callback function. A callback function is executed after the current effect is finished.

SYNTAX
$(selector).hide(speed,callback);

Example (with callback)
It has a callback parameter that is a function that will be executed after the hide effect is completed.
$("button").click(function() {
  $("p").hide("slow", function() {
    alert("The paragraph is now hidden");
  });
});


Example (without Callback)
It has no callback parameter, and the alert box will be displayed before the hide effect is completed.
$("button").click(function() {
 $("p").hide(1000);
 alert("The paragraph is now hidden");
});

No comments:

Post a Comment

Note: only a member of this blog may post a comment.