Espere até que uma função com animações termine até executar outra função
Estou tendo um problema com normal (não-ajax) funções que envolvem muitosanimações dentro de cada um deles. Atualmente eu simplesmente tenho umsetTimeout
entre funções, mas isso não é perfeito, já que nenhum navegador / computador é o mesmo.
Nota adicional: Ambos têm animações separadas / etc que colidem.
Eu não posso simplesmente colocar um na função de retorno de chamada de outro
// multiple dom animations / etc
FunctionOne();
// What I -was- doing to wait till running the next function filled
// with animations, etc
setTimeout(function () {
FunctionTwo(); // other dom animations (some triggering on previous ones)
}, 1000);
Existe de qualquer maneira em js / jQuery para ter:
// Pseudo-code
-do FunctionOne()
-when finished :: run -> FunctionTwo()
Eu sei sobre$.when()
& $.done()
, mas esses são para AJAX ...
O jQuery tem uma variável exposta (que, por algum motivo, não está listada em nenhum lugar nos documentos do jQuery) chamada $ .timers, que contém a matriz de animações que estão ocorrendo atualmente.
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);
};
Uso básico:
// 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();
});