I'm building a javascript application and there is sometimes a need to do a trigger-click. Calling the trigger, the app wait once for an animation end and run a few lines codes after that. But the script doesn't wait until the animation end and continues with other code.
The problem is hard to describe, so here's my code:
var $output = $('#output');
$('#link-1').click(function(){
$('#link-2').trigger('click');
$output.append('<div>Second output</div>');
});
$('#link-2').click(function(){
var $obj = $(this);
$obj.toggleClass('active');
$obj.one('transitionend', function(){
$output.append('<div>First output</div>');
});
});
The result looks like:
<div id="output">
<div>Second output</div>
<div>First output</div>
</div>
You can try it here: http://jsfiddle.net/MKZvZ/1/
How can i achieve that First output
appears before Second output
?
First output
and Second output
are just basic examples, in effect the app doe's some logic work.