I have two functions here say animation1 and load1 they work like that:
function animation1() {
element.animate({width(size)});
}
and
function load1() {
element.find('img').after(newImg)
}
there’s a click event who fires both, but how can I tell to load1 to wait until animation 1 has finished the animation and then add the newImg element? I know I can callback load1 into animate() of animation1, but I’ve separate one from another because I’m reusing animation1 in a lot of places. What happens now is that both are fired together, and it’s not nice to the eyes.
thank you
d.
Two options:
Use a callback
I know you’ve said
…but a callback really is the best way to do this. In the places where you don’t need a callback, just don’t pass one to
animation1.Modify
animation1to accept a callback, which it passes to theanimatefunction, which accepts a “completion” callback it fires when the animation is complete:…and then your click would pass in a reference to
load1toanimation1rather than calling it directly, like this:See the
animatedocs for details of the completion function.Use a timer
If you tell
animation1exactly how long to take, you could usesetTimeoutto delay your call toload1:…and then your click would pass in a duration to
animation1and usesetTimeoutto delay callingloadby the same amount:Of the two, I’d definitely go for the callback rather than the
setTimeoutbarring some other design constraint.