确定何时jQuery load()完成了多个图像的加载
Identify when jQuery load() has finished loading multiple images
我有一个脚本,使用jQuery的getScript()
函数从tumblr帐户动态加载博客文章。
脚本响应是一个JSON结构tumblr_api_read
,其中包含(除其他外)图像url。
function read(blogName) {
var imageUrls = [];
$.getScript("http://" + blogName + ".tumblr.com/api/read/js", function() {
imageUrls = getImageUrls(tumblr_api_read.posts); // returns an array of URLs to use as img src attributes
}).done(function(script, textStatus) {
loadImages(imageUrls);
processTumblrResponse(); // ### ONLY EXECUTE WHEN IMAGES ARE LOADED!!
});
}
我在函数loadImages()中使用imageUrls数组,通过jquery构建的var $temporaryImageContainer
将<img/>
s注入DOM,然后进行一些进一步的处理。
var imgAjaxComplete = false;
var imgCount = 0;
function loadImages(imageUrls, $temporaryImageContainer) {
while (!imgAjaxComplete) {
for (srcKey in imageUrls) {
var $image = $("<img>", {"id" : "imageUrl" + srcKey, "src" : imageUrls[srcKey]})
.load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
console.log("Error loading image: " + imageUrls[srcKey]);
} else {
$temporaryImageContainer.append($image);
console.log("Loaded image: " + imageUrls[srcKey]);
}
});
++imgCount;
}
if (imgCount == imageUrls.length) {
imgAjaxComplete = true;
imgCount = 0;
}
}
console.log("loadImages() complete");
}
function processTumblrResponse() {
console.log("Images loaded. Running processTumblrResponse()");
// further processing here...
}
这个问题是loadImages()
中的for
循环在其他任何事情之前执行(我认为这就是正在发生的事情),它只尝试加载imageUrls
中的最后一个图像src n次(其中n是imageUrls.length
)。
是否有可能在我执行额外处理之前识别注入DOM的所有图像何时完成加载?
JSON太大了,不能在这里发布,但如果需要上下文,请告诉我。
将函数loadImages
替换为这里:
function loadImages(imageUrls, $temporaryImageContainer) {
for (srcKey in imageUrls) {
var $image = $("<img>", {"id" : "imageUrl" + srcKey, "src" : imageUrls[srcKey]})
.load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
console.log("Error loading image: " + imageUrls[srcKey]);
} else {
$temporaryImageContainer.append($image);
console.log("Loaded image: " + imageUrls[srcKey]);
}
imgCount++;
checkIfImagesAreLoaded();
});
}
}
function checkIfImagesAreLoaded() {
if (imgCount === imageUrls.length) {
imgCount = 0;
imgAjaxComplete = true;
console.log("loadImages() complete");
}
}
至于解释,for循环在其他任何事情之前完成,因为jQuery load
函数只在加载图像时执行作为参数传入的方法,所以它不会立即被调用。
您不应该在Javascript中使用for (x in array)
,因为该循环用于迭代对象的属性。不幸的是,最佳实践是使用for (var i = 0; i < length; i++) ...
。
查看W3Schools的循环文档。
你也可以使用:
yourArray.forEach(function(item) {
//Do crazy stuff here
});
但是forEach()
是ES5的一部分,在IE8及以下的旧浏览器中是不支持的。
相关文章:
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- 在检索数据时是否可以停止图像加载请求
- 如何让我的网站上的WEBP图像加载在morzilla firefox中有一个可能的解决方案吗?如果是,那么如何
- jQuery:获取图像加载错误的详细信息
- 在safari和chrome中,js图像加载产生了奇怪的结果
- 图像加载不适用于 IE 8 或更低版本
- 这个图像加载功能有什么作用
- JavaScript 图像加载器不起作用
- 动态图像加载是否会减少服务器过载
- 跨来源图像加载被拒绝-仅限iPhone-简单图像上传
- 纯javascript的进度图像加载程序仅当用户单击提交html表单时
- 使用JavaScript延迟图像加载
- 如果当前图像加载缓慢,如何加载其他图像
- HTML图像加载触发一个错误
- 将图像加载到localStorage,并将图像src设置到该位置
- jquery图像加载检查错误
- 将图像加载到织物画布背景中
- 需要在bootstrap css中单击时将图像加载为模态
- 正在显示加载..在将新图像加载到网站时播放动画
- 砖石事件:在图像加载和布局完成后调用事件