I want to check if each image is loaded and then finally on complete fire another function and remove a div. But I don't know how to check if the image has loaded completely
Fundamentally, checking for image load success is simple:
var theImage = new Image();
var element = d[i].split('"');
$(theImage).load(function() {
// The image has loaded (from cache, or just now)
// ...do something with the fact the image loaded...
});
theImage.src = '/karlaacosta/images/Fotografia/TODO'+element[0];
The key thing above is to hook the load
event before you set its src
. If you set src
first, and the image is in cache, the load
event can fire before the next line of code hooking it, and you'll miss it.
Here's an example: Live copy | source
HTML:
<img src="http://www.gravatar.com/avatar/ca3e484c121268e4c8302616b2395eb9?s=32&d=identicon&r=PG">
<p>When you see the image above,
<input type="button" id="theButton" value="Click Me"></p>
JavaScript:
jQuery(function($) {
$("#theButton").click(function() {
var img = document.createElement('img');
$(img).load(function() {
display("Image <code>load</code> event received");
});
img.src = "http://www.gravatar.com/avatar/ca3e484c121268e4c8302616b2395eb9?s=32&d=identicon&r=PG";
document.body.appendChild(img);
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
As you can see, I'm using the same img src
in each case. And reliably on every browser I've ever tried, I do receive the load
event.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…