English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery Callback Functions (Callback)

JavaScript statements are executed line by line. However, since jQuery effects require some time to complete, the next code line may execute before the previous effect is still running, which may cause errors.

To prevent this from happening, jQuery provides a callback function for each effect method.

The callback function will be executed after the current effect is completed.

Callback functions are passed as parameters to effect methods and are usually displayed as the last parameter of the method.

Typical Syntax: $(selector).hide(duration, easing, callback);

The following example has a callback parameter, which is a function to be executed after the hide effect is completed:

$("button").click(function(){
  $("p").hide("slow", function(){
    // Code to be executed after the effect is completed
    alert("The paragraph is now hidden");
  });
});
Test and See‹/›

The following example does not have a callback parameter and will display an alert box before the hide effect is completed:

$("button").click(function(){
  $("p").hide("slow");
     alert("The paragraph is now hidden");
});
Test and See‹/›