I'm using the following Javascript functions to display a gallery of pictures.
function disp_pics(currObj,table){
if(currObj != "none"){
$("div .checkout2").removeClass("checkout2").addClass("checkout");
$(currObj).closest("div").removeClass("checkout").addClass("checkout2");
}
function getData(table){
return $.ajax({
url: "newphoto_gallery_display.php",
type: "GET",
data: {
table: table
},
dataType: "html"
});
}
function display_result(data){
var dfd = new $.Deferred();
dfd.done(equalise);
$("#ajaxoutput").html(data);
setTimeout(function() {
dfd.resolve();
}, 1000);
}
function equalise(){
var highestBox = 0;
$('.photodisplay').each(function(){
if($(this).height() > highestBox) {
highestBox = $(this).height();
}
});
$('.photodisplay').height(highestBox);
}
var promise=getData(table);
promise.success(function (data) {
promise.done(display_result(data));
});
};
The function getData fetches the picture data from a database. The function display_result then outputs that data to the div id "ajaxoutput". Pictures are displayed along with relevant data in boxes (HTML tables with a border). The function equalise is then called to make all the boxes of equal height.
Without the time delay in the display_result the equalise function is called before all the pictures and data have been displayed and therefore messes up the display. Is there a way to do away with the time delay and only call equalise function when display_result has finished outputting all the data to the ajaxoutput div?
I've tried all sorts of Deferred's and $.when.......then.... combinations, but haven't managed to achieve the desired result without delaying the script, which isn't ideal. Any suggestions would be welcome.