Callback Functions:
It is used to prevent executing further code until earlier effects has not executed completely. As we know in jQuery effects takes time to complete and sometime next line code get executed while the earlier effects are still executing.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Callback Functions</title>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").slideToggle("slow", function(){
alert(" Effect completed successfully.");
});
});
});
</script>
</head>
<body>
<div class="container">
<p>Demo of Callback Function in jQuery</p>
<div style="text-align:center;"><button type="button">Click me</button></div>
</div>
</body>
</html>
In the above example we used alert() statement inside callback function for slideToggle() method. And when we run the above code the message written inside alert() statement will be displayed only when the slide toggle effect has completed.
You can check the output of above example here: https://jsfiddle.net/7abbqqyf/1/
0 Comment(s)