← Back to jQuery Course | Chapter 4: Effects & Animations | Lesson 6 of 9

Animation Callback Functions

एक callback किसी animation खत्म होने के बाद चलती है।
Syntax
javascript
$(selector).effect(speed, function () {
  // runs after the effect completes
});

Completion Callback

एक completion callback एक function है जिसे किसी effect method के आख़िरी argument के रूप में pass किया जाता है, और jQuery इसे सिर्फ उस specific animation के खत्म होने के बाद चलाता है।

उदाहरण: Completion Callback

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Box</div>
    <script>
      $("#box").fadeOut(1000, function() {
      console.log("Fade complete");
      });
    </script>
  </body>
</html>

animate() Callback

animate() भी simpler effect methods की तरह ही अपने आख़िरी argument के रूप में एक callback accept करता है, इसे तभी चलाते हुए जब यह control करने वाली property transitions पूरी तरह complete हो जाएँ।

उदाहरण: animate() Callback

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px">Box</div>
    <script>
      $("#box").animate({width: "300px"}, function() {
      console.log("Animation complete");
      });
    </script>
  </body>
</html>

एक और Action चलाना

Callbacks किसी animation के अंत में behavior chain करने का standard तरीका हैं -- उदाहरण के लिए किसी element को DOM से सिर्फ तभी हटाना जब वह fade out खत्म कर चुका हो, तुरंत हटाकर animation को बीच में काटने के बजाय।

उदाहरण: Run Another Action

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Box</div>
    <script>
      $("#box").fadeOut(1000, function() {
      $(this).remove();
      });
    </script>
  </body>
</html>

Callback में this

किसी jQuery animation callback के अंदर, keyword this उस raw DOM element को refer करता है जो animate किया गया था, कोई jQuery object नहीं, इसलिए उस पर दूसरी jQuery methods call करने से पहले इसे आमतौर पर $(this) में wrap करना पड़ता है।

उदाहरण: Callback this

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" data-name="mybox">Box</div>
    <script>
      $("#box").fadeOut(1000, function() {
      console.log($(this).data("name"));
      });
    </script>
  </body>
</html>

Callback और Queue

किसी completion callback के अंदर से एक दूसरा animation शुरू करना आपको multi-step sequences बनाने देता है, जैसे किसी element को ऊपर slide करना फिर उसे fade out करना, बिना दोनों effects एक साथ चलाए।

उदाहरण: Callback and Queue

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Box</div>
    <script>
      $("#box").slideUp(500, function() {
      $(this).fadeOut(500);
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. callback के बजाय .fadeOut(1000) के बाद follow-up code लिखना, इसलिए यह animation अभी भी चलते हुए तुरंत चल जाता है।
  2. callback के रूप में एक arrow function इस्तेमाल करना और फिर $(this) पर निर्भर रहना, जो अब animated element को refer नहीं करता।
  3. callback को parentheses के साथ pass करना जैसे .fadeOut(1000, done()), जो animation खत्म होने के बजाय done को तुरंत चला देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.