Poczekaj, aż funkcja z animacjami zostanie zakończona, dopóki nie uruchomisz innej funkcji
Mam problem z normalnym (nie-ajax) funkcje, które obejmują wieleanimacje w każdym z nich. Obecnie po prostu mamsetTimeout
między funkcjami, ale nie jest to idealne, ponieważ żadne przeglądarki / komputery nie są takie same.
Dodatkowa uwaga: Oboje mają osobne animacje / itp., Które kolidują.
Nie mogę po prostu umieścić jednego w funkcji wywołania zwrotnego innego
// 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);
Czy w js / jQuery jest jeszcze:
// Pseudo-code
-do FunctionOne()
-when finished :: run -> FunctionTwo()
wiem o$.when()
& $.done()
, ale te są dla AJAX ...
jQuery ma odsłoniętą zmienną (która z jakiegoś powodu nie jest umieszczona w żadnym miejscu w dokumentach jQuery) o nazwie $ .timers, która przechowuje tablicę aktualnie przeprowadzanych animacji.
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);
};
Podstawowe użycie:
// 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();
});