猛跑小豬
2019-11-25 14:34:48
我在正常(非ajax)函數中遇到了問題,每個函數中都包含很多動畫。目前,我只是具有一個setTimeoutbetween函數,但這并不是完美的,因為沒有瀏覽器/計算機是相同的。附加說明:它們都有碰撞的單獨動畫/等。我不能簡單地將一個放在另一個的回調函數中// multiple dom animations / etcFunctionOne();// What I -was- doing to wait till running the next function filled// with animations, etcsetTimeout(function () { FunctionTwo(); // other dom animations (some triggering on previous ones)}, 1000); 無論如何在js / jQuery中有:// Pseudo-code-do FunctionOne()-when finished :: run -> FunctionTwo()我知道$.when()&$.done(),但是這些是針對AJAX的...我更新的解決方案jQuery有一個名為$ .timers的暴露變量(由于某種原因未在jQuery文檔中的任何地方列出),該變量保存當前發生的動畫數組。function animationsTest (callback) { // Test if ANY/ALL page animations are currently active var testAnimationInterval = setInterval(function () { if (! $.timers.length) { // any page animations finished clearInterval(testAnimationInterval); callback(); } }, 25);};基本用法:// run some function with animations etc functionWithAnimations();animationsTest(function () { // <-- this will run once all the above animations are finished // your callback (things to do after all animations are done) runNextAnimations();});
3 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
在第一個函數的末尾添加以下內容
return $.Deferred().resolve();
像這樣調用兩個函數
functionOne().done(functionTwo);

Helenr
TA貢獻1780條經驗 獲得超4個贊
除了Yoshi的答案,我還找到了另一個非常簡單的動畫(回調類型)解決方案。
jQuery有一個公開變量(由于某種原因未在jQuery文檔中列出),稱為$ .timers,該變量保存當前正在發生的動畫數組。
function animationsTest (callback) {
// Test if ANY/ALL page animations are currently active
var testAnimationInterval = setInterval(function () {
if (! $.timers.length) { // any page animations finished
clearInterval(testAnimationInterval);
callback();
}
}, 25);
};
基本用法:
functionOne(); // one with animations
animationsTest(functionTwo);
希望這可以幫助一些人!
添加回答
舉報
0/150
提交
取消