我想調用一些函數,但等待前一個已完成。 我知道jQuery在幾個函數中提供了一個回調參數,但我想了解如何在我自己的jQuery插件中實現此行爲。所以這樣的話:我怎樣才能調用一些javascript函數,但是,等待之前已經完成了?
從my previous question讀答案後,我寫了這個:
(function(callback){
$('#art1').animate({'width':'1000px'},1000);
callback();
})((function(callback2){
$('#art2').animate({'width':'1000px'},1000);
callback2();
})(function(){
$('#art3').animate({'width':'1000px'},1000);
}));
但不工作。我再次嘗試,並寫道:
animate1(function(){
animate2(function(){
animate3();
})
});
function animate1(callback){
$('#art1').animate({'width':'1000px'},1000);
callback();
}
function animate2(callback){
$('#art2').animate({'width':'1000px'},1000);
callback();
}
function animate3(callback){
$('#art3').animate({'width':'1000px'},1000);
callback();
}
但仍然無法正常工作。三個動畫仍然在同一時間開始。我希望他們被稱爲一個接一個。但是,如果沒有使用:
$('#art1').animate({'width':'1000px'},1000,'linear',function(){
$('#art2').animate({'width':'1000px'},1000,'linear',function(){
$('#art3').animate({'width':'1000px'},1000);
});
});
你有沒有考慮過使用'$ .delay()'?不知道它是否會做到你想要的。否則,我認爲將每個動畫作爲回調傳遞給該動畫是它的前進方向。 – 2011-03-04 15:23:09
@Brian Driscoll:我不知道需要多少時間。我不能使用$ .delay() – texai 2011-03-04 15:30:04