function1 = function(){
something.on('transitionend', function(){
// now function2 should run
});
}
function2 = function(){
alert('ok');
}
function1();
function2();
So I heard about jQuery promises. I would return a "deferred" object, and inside the event handler I would call deferred.resolve();
But what happens if i have multiple event handlers there and I only want the next function to run when all have been fired? + I don't like the idea of introducing something foreign like "deferred" into other parts of the code.
Is there any other way to detect if function1 has finished all its work?
Try this,
Here fuction1 is your first function to call, and fuction2 is your second function.
Note,
transitionend
event may fire multiple times ifall
is set withincss
transition
property valueTry (this pattern)
i.e.g.,
html
css
js
jsfiddle http://jsfiddle.net/guest271314/u7B9K/
Either you take the promise approach, or you take the callback approach.
With callbacks, you'd pass
function2
as a parameter tofunction1
;... but then you get nested-hell if you have
function3
dependant onfunction2
, andfunction4
dependant on 3.This is why you'd go down the deferred route;
... which would allow you to chain successive functions rather than nesting them (providing they all returned promises, of course).
Combining event handlers and deferreds is a bit messy. So if you went down the route of having multiple event handlers, you'd end up having to do something lame such as;
The real way of combining multiple deferreds is by using
$.when()
, but unfortunately here you don't have multiple deferreds, and adding them will be as messy as using themaybeFire
approach.