我一直在阅读一些有关排队 JS 回调的文章,但没有将它们堆叠在这里, How do I animate in jQuery without stacking callbacks?
我尝试过使用 Julian Aubourg 的答案,我注意到从函数中删除“return”语句后,一切都会立即发生。
我只是想知道这些 return 语句的行为是什么,以及它们在使此代码同步方面发挥什么作用。
$('div1').fadeOut('slow').promise().pipe(function() {
return $('div2').fadeOut('slow');
}).pipe(function() {
return $('div3').animate({ top: 500 }, 1000 );
});
最佳答案
现在,所有 jQuery 动画都会返回自己的 promise ,因此基本上您会返回一个 promise ,当您这样做时,该 promise 会在动画完成时得到解决:
return $('div2').fadeOut('slow');
这相当于
var def = new $.Deferred();
$('div2').fadeOut('slow', function() {
def.resolve();
});
return def.promise();
关于javascript - 何时返回 jQuery 函数而不是仅仅调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20406249/