jQuery Callback

jQuery Callbacks is a key concept that helps you run code after an animation or effect finishes.


What is a jQuery Callback?


A callback is a function that is executed after the current effect is complete.


In jQuery, many effects like .hide(), .show(), .slideDown(), .fadeOut(), .animate(), etc. allow you to pass a callback function as the last parameter.


Syntax:


$(selector).effect(duration, callback);


Or simply:


$(selector).effect(callback);


The example below has a callback parameter that is a function that will be executed after the hide effect is completed:

Example
<script>
$(document).ready(function(){
$("#hideBtn").click(function(){
$("#box").hide(1000, function(){
alert("Box is now hidden!");
});
});
});
</script>

Try it yourself

The example below has no callback parameter, and the alert box will be displayed before the hide effect is completed:

Example
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
});
</script>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.