How to call a function after a fadeOut() on many elements

后端 未结 2 1880
-上瘾入骨i
-上瘾入骨i 2020-12-25 13:32

I have this code :

$(\'.hotel_photo_select\').fadeOut(500, function () {
    alert(\"Now all \'.hotel_photo_select are hidden\'\");
});

and

相关标签:
2条回答
  • 2020-12-25 13:38

    You can use the promise() method for this (the doc page has a good example for this).

    The .promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended.

    Applied to your example should be something like this:

    $.when($('.hotel_photo_select').fadeOut(500))
                                   .done(function() {
        alert("Now all '.hotel_photo_select are hidden'");
    });
    
    0 讨论(0)
  • 2020-12-25 13:55

    Using jQuery $.when().then() functions.

    $(document).ready(function(){
      // using When & then methods.
      $.when($('.box').fadeOut())
       .then(function(){
          alert("All Boxes : Faded Out.");
       });
    });
    .box{
        color: white;
        background-color: red;
        width: 100px;
        height: 100px;
        text-align: center;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <html>
      <head>
        <title>Calling Alert After fadeOut</title>
      </head>
      <body>
        <div class="box">Box 1</div> <br />
        <div class="box">Box 2</div> <br />
        <div class="box">Box 3</div> <br />
        <div class="box">Box 4</div> <br />
        <div class="box">Box 5</div>
      </body>
     </html>

    0 讨论(0)
提交回复
热议问题