jQuery Callback Functions for ASP.NET Applications
As displayed earlier in jQuery Special Effects, many jQuery functions involve animations. These animation functions may take effect durationor speed of execution as an optional parameter value. For example:
$("#btnHideText").click(function(){
$("#txtName").hide(2000);
});
Since JavaScript and, therefore, jQuery code statements are executed in a sequence line by line, instructions executed after a special effect may create errors because the animation has been completed. To avoid this problem, a .NET developer can add a Callback Function as a parameter to the jQuery method. The jQuery callback function will be executed after the animation has completely finished. The form of the syntax usually is:
$(selector).hide(speed,callback)
In the case above, the callback parameter is a function executed after jQuery completes the "hide" special effect.
$(document).ready(function(){
$("#btnHideText").click(function(){
$("#txtName").hide(1000,function(){
alert("The name text box is now hidden");
});
});
});
In the example above, when the button "btnHideText" is clicked, the "txtName" text box fades, and when it is completely hidden, the jQuery callback function displays the alert box message "The name text box is now hidden."