完成每个功能后如何调用其他功能?

时间:2015-08-03 07:42:05

标签: javascript jquery

$('.chide').each(function(index) {
        setTimeout(function(el) {
        el.animate({opacity:0});
       }, index * 200, $(this));
 });

我想在完成上述功能后运行另一个功能,我该怎么做才能帮助..

2 个答案:

答案 0 :(得分:0)

我认为这是你可以在这里实现的解决方案

$('.chide').each(function(index) {
        setTimeout(function(el) {
        el.animate({
          opacity:0,
          complete: function(){
                       alert('call your another function here you can write here with return as well can create a function here aswell you can just call the function here that is created ');
                    } 
        });
       }, index * 200, $(this));
 })

这里我创建了使用divs

的工作代码示例

$(document).ready(function(){
    $('div').each(function(index) {
       console.log(index);
       $(this).animate({
          opacity:1,
          height: "toggle",          
       }, index * 2200,function(){
         $(this).show();
       });       
    })
})
div{
	width : 100px;
	height: 100px;
	border: 2px solid grey;
}
<div>

	</div>
	<div>
		
	</div>
	<div>
		
	</div>
	<div>
		
	</div>
	<div>
		
	</div>

答案 1 :(得分:0)

如果您的应用/网站可以使用现代功能,请使用Promises。

Promise.all(arrayOfPromises).then(function(arrayOfResults) {
  //...
});

它是一种组织代码的理智方式,可以使用一些固体填充:http://www.html5rocks.com/en/tutorials/es6/promises/

相关问题