[英]How to wait until a loop of jquery effects are finished before executing function
[英]How to wait until jQuery effects have finished?
我怎樣才能等到動畫在jQuery中完成?
function foo() {
$("#element").fadeOut();
}
我需要.fadeOut()
在foo()
返回之前完成,因為調用代碼期望DOM不含#element
。
所有jQuery動畫函數都有complete
回調。 因為它們是異步的,所以您不能等待它們,因此必須使用回調。
function foo() {
$("#element").fadeOut(function() {
// finished
});
}
jQuery將淡出定義為: .fadeOut( [duration ] [, complete ] )
,因此使用回調應該可以正常工作。
complete類型:Function()動畫完成后調用的函數,每個匹配的元素調用一次。
為此使用回調函數
function foo() {
$("#element").fadeOut('slow', function(){
// called when the element finishes fading out
});
}
您無法阻止foo
在動畫結束之前返回。 您需要使用回調或promise / deferred模式。 這是回調的工作方式:
function bar() {
foo(function() {
// stuff to execute after foo()
});
}
function foo(callback) {
$("#element").fadeOut('speed', callback);
}
$('#element').fadeOut(1000, function() {
// after 1 second do something
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.