gpt4 book ai didi

javascript - 加载列表(带图像)时添加回调函数

转载 作者:行者123 更新时间:2023-12-03 11:25:08 25 4
gpt4 key购买 nike

文档准备好后,我通过 ajax 调用加载此图像列表:

$.ajax({
url:'mysite.it/ajax/list_filter.php',
data:data,
type:'POST'
}).done(function(data){
var response = $.parseJSON(data);
var people = '';
if(response.success){
if(response.showcase){
$.each((response.showcase),function(index,item){
people += '<li>';
people += '<img src="'mysite.it/pict_thumbs/' +item.pool_user_pict+ '">';
people += '<label>' +item.username+ '</label>';
people += '</li>';
});
}
}
});

由于列表可能很大,我只想在所有图像加载并显示并且列表准备就绪后调用回调函数。
我该怎么做??

最佳答案

function loadImages(images, callback) {
var length, count, check, i, img;

length = images.length;
count = length;

check = function () {
if (--count === 0) {
callback();
}
};

for (i = 0; i < length; i++) {
img = new Image();

img.onload = check;
img.onerror = check;

img.src = images[i]
}
}

var images = [], html = '';

$.each(response.showcase, function(index, item) {
html += '<li>';
html += '<img src="mysite.it/pict_thumbs/' + item.pool_user_pict + '">';
html += '<label>' + item.username + '</label>';
html += '</li>';

images.push(item.pool_user_pict);
});

loadImages(images, function () {
$('#some-element').html(html);
});

JSFiddle:http://jsfiddle.net/v2dyfdyu/

关于javascript - 加载列表(带图像)时添加回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com