How to stop firing event until an effect is finished in jQuery
Topic: JavaScript / jQueryPrev|Next
Answer: Use the jQuery callback function
You can utilize the jQuery callback function mechanism to stop events from firing until an effect is completely finished, such as doing something after slide animation or fade out transition is fully completed and so on. Let's take a look at an example to see how it works:
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Fire Events after an Effect is Over</title>
<style>
.box{
height: 300px;
display: none;
background: yellowgreen;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$(".box").slideDown(3000, function(){
$(".hint").text("Sliding is completed, now fading out.");
$(this).fadeOut(3000, function(){
$(".hint").text("Fading out completed.");
});
});
});
</script>
</head>
<body>
<div class="box"></div>
<p class="hint"></p>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: