admin管理员组文章数量:1289634
I checked other questions, but they all had information how to make a callback when one specific image has loaded:
var img = new Image();
img.src = "images/img.png";
if (!imgplete) {
img.onload = function() {
// code to be executed when the image loads
}
}
Or a simple check to see if all images are loaded with an 'if' statement. Also, $(window).load (or onLoad or whatever) doesn't work.
In my case I'm loading two images:
var img1 = new Image();
var img2 = new Image();
img1.src = 'images/img1.png';
img2.src = 'images/img2.png';
How can I define a callback similar to the one in the first example, but it gets executed when BOTH images have finished loading?
Thank you for your time.
I checked other questions, but they all had information how to make a callback when one specific image has loaded:
var img = new Image();
img.src = "images/img.png";
if (!img.plete) {
img.onload = function() {
// code to be executed when the image loads
}
}
Or a simple check to see if all images are loaded with an 'if' statement. Also, $(window).load (or onLoad or whatever) doesn't work.
In my case I'm loading two images:
var img1 = new Image();
var img2 = new Image();
img1.src = 'images/img1.png';
img2.src = 'images/img2.png';
How can I define a callback similar to the one in the first example, but it gets executed when BOTH images have finished loading?
Thank you for your time.
Share Improve this question asked Jan 19, 2012 at 6:09 Nikolay DyankovNikolay Dyankov 7,24411 gold badges65 silver badges89 bronze badges2 Answers
Reset to default 11Here's a function that will create several images and call a callback when they are all loaded:
function createImages(srcs, fn) {
var imgs = [], img;
var remaining = srcs.length;
for (var i = 0; i < srcs.length; i++) {
img = new Image();
imgs.push(img);
img.onload = function() {
--remaining;
if (remaining == 0) {
fn(srcs);
}
};
img.src = srcs[i];
}
return(imgs);
}
var imgs = createImages(['images/img1.png', 'images/img2.png'], myCallback);
P.S. whenever working with .onload
for images, you must set the .onload
handler before setting the .src
value because the onload handler might fire immediately when setting the .src value if the image is in the cache. If you haven't set the onload handler first, then it may never fire because by the time you set the handler, the image is already loaded. This happens in some browsers. Just always set .onload
before .src
if you need the onload event.
It's called reference counting. It's the standard technique for running a single callback after n tasks have finished.
var count = 2;
img1.onload = function () {
count-- === 0 && callback();
}
img2.onload = function () {
count-- === 0 && callback();
}
function callback() {
}
本文标签: javascriptCrossbrowser solution for a callback when loading multiple imagesStack Overflow
版权声明:本文标题:javascript - Cross-browser solution for a callback when loading multiple images? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477729a2380984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论