简体   繁体   English

将操作推迟到.each()完成之前

[英]Deferring action until .each() has completed

I have the following method: 我有以下方法:

function animatePortfolio(fadeElement) {
    fadeElement.children('article').each(function(index) {
        $(this).delay(1000*index).fadeIn(900);
    });
}

I'd like to defer an action until .each() has entirely completed. 我想将操作推迟到.each()完全完成之前。 Assume this is some version of deferred/promise I need to employ, but not understanding how in this scenario it would work. 假设这是我需要使用的某些版本的延迟/承诺,但不了解在这种情况下它将如何工作。

this can be done with done function 这可以通过done功能来完成

function animatePortfolio(fadeElement) {
   fadeElement.children('article').each(function(index) {
      $(this).delay(1000*index).fadeIn(900);
  }).promise().done(function(){
     // do aditional animation here 
   });
 }

OR 要么

when function called do it there 当函数调用在那里

    animatePortfolio(fadeElement).promise().done(function(){

     // do things here 
    });

You can use the promise object returned by animation 您可以使用动画返回的Promise对象

 function animatePortfolio(fadeElement) { fadeElement.children('article').each(function(index) { $(this).delay(1000 * index).fadeIn(900); }).promise().done(function() { fadeElement.css('color', 'red') }); } animatePortfolio($('div')) 
 article { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <article>1</article> <article>2</article> <article>3</article> <article>4</article> </div> 

Do not use done with animation promises, as if any are in the target state to begin with it will not fire. 不要将done与动画承诺一起使用,因为任何处于目标状态的动画都不会触发。 Use always() instead 使用always()代替

function animatePortfolio(fadeElement) {
   fadeElement.children('article').each(function(index) {
      $(this).delay(1000*index).fadeIn(900);
  }).promise().always(function(){
     // do aditional animation here 
   });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM