我正在使用以下Javascript函数来显示图片库.
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));
});
};
Run Code Online (Sandbox Code Playgroud)
函数getData从数据库中获取图片数据.函数display_result然后将该数据输出到div id"ajaxoutput".图片与相关数据一起显示在框中(带边框的HTML表格).然后调用函数equalize以使所有框具有相等的高度.
如果没有display_result中的时间延迟,则在显示所有图片和数据之前调用均衡功能,从而弄乱显示器.有没有办法消除时间延迟,只有当display_result已经完成将所有数据输出到ajaxoutput div时才调用均衡函数?
我已经尝试了各种各样的Deferred和$ .when .......然后......组合,但是没有设法在不延迟脚本的情况下达到预期的结果,这是不理想的.欢迎大家提出意见.