Thursday 28 April 2011

jQuery Callback Function


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

jQuery Callback Functions

A callback function is executed after the current animation (effect) is finished.
JavaScript statements are executed line by line. However, with animations, the next line of code can be run even though the animation is not finished. This can create errors.
To prevent this, you can create a callback function. The callback function will not be called until after the animation is finished.

jQuery Callback Example

Typical syntax: $(selector).hide(speed,callback)
The callback parameter is a function to be executed after the hide effect is completed:

Example with Callback

$("p").hide(1000,function(){
  alert("The paragraph is now hidden");
});
Without a callback parameter, the alert box is displayed before the hide effect is completed:

Example without Callback

$("p").hide(1000);
alert("The paragraph is now hidden");

No comments:

Post a Comment