I hope you can help me... I want to do exactly, what nth Child asked four years ago in the following contribution jquery, how to run a function as soon as another function ends . I know this problem has been solved earlier. I found the solutions here, and after long sessions of finding out how to approach and now a huge stack of frustration, I can't help other than opening this topic again. So here ist what I would like to do:
Basically I want to define the behavior of content to be toggled in and / or out when clicking of elements (reference boxes). I have two functions:
1.function 'removeRefContent'
is meant for checking, whether content of other references, than the one clicked are opened, and if yes, close
function: 'appendRefContent2EndOfLine'
just adds content of the clicked reference box and displays it.
Here are the essential code snippets:
function displayRefContent ( clickedRefID ) {
// check, if element is opened already
if ( $( 'div#' + clickedRefID + '_refAdditionalContent' ).length )) {
removeRefContent();
} else {
// check whether other element is opened and close
removeRefContent().then( appendRefContent2EndOfLine ( clickedRefID ) );
}
}
// these two functions are behind
function appendRefContent2EndOfLine ( requestedRefID ) {
// get refContent from reference storage container
var refContent = '<div id="' + requestedRefID + '_refAdditionalContent" class="refAdditionalContent">';
refContent = refContent + $( 'div#' + requestedRefID + '_additionalContentStorageContainer' ).html();
refContent = refContent + '</div>';
// find last refPreviewImage in elements row and store in variable lastElementInRowID
var lastElementInRowID = 0; //... some more code to find lastElementInRowID ...
// add refContent after last element in row and toggle in
$( 'div#' + lastElementInRowID + '_ref' ).after( refContent );
$( 'div#' + requestedRefID + '_refAdditionalContent' ).slideToggle( 500 );
}
var removeRefContent = function () {
var removeRefContentDeferred = $.Deferred();
$( 'div.refAdditionalContent' ).slideToggle( 500 , function() {
$( 'div.refAdditionalContent' ).remove( function() {
removeRefContentDeferred.resolve();
});
});
return removeRefContentDeferred.promise();
}
Both functions do what they are meant for. The issue occuers, when line
removeRefContent().then( appendRefContent2EndOfLine ( clickedRefID ) );
is requested. I want to first remove the content of all other references (by toggle out), and only when finished and $( 'div.refAdditionalContent' ).remove is done, the other function to appendRefContent2EndOfLine shall be executed.
I read lots of stuff about asynchronuous and queueing and sequences, but simply do not understand, why my solution does not work. The result is, that both functions are executed at the same time leading to conflicts.
So far i consider among many other solution approaches the one I've chosen as the most elegant one. But I am very open to learn about your ideas...
Thank you guys!
Cheers Frank