I would like to know if there is any way to know when the browser has finished processing an image after loading it, let me explain:
When you enter an image, you can know when it has finished loading it with the property complete
or the method onload
of the object, but between the loads and displays it, it takes some time, since it must process it (for example, resize it), so that in "very" large images, even if the browser claims to have finished loading the image, it may not be displayed immediately . Here I leave an example of what I mean, by log it shows a message when it loads, but it takes a while for the image to appear.
Example using the method onload
:
var imagen;
ponImagenTocha();
function ponImagenTocha(){
imagen=document.createElement("IMG");
imagen.height=200;
imagen.src="http://orig09.deviantart.net/5e53/f/2013/347/f/d/i_don_t_want_to_ever_leave_the_lake_district_by_martinkantauskas-d6xrdch.jpg";
imagen.onload=function(){esperarImagen();};
}
function esperarImagen(){
console.log("Completo.");
document.body.appendChild(imagen);
}
Example with the property complete
:
var imagen;
ponImagenTocha();
function ponImagenTocha(){
imagen=document.createElement("IMG");
imagen.height=200;
imagen.src="http://orig09.deviantart.net/5e53/f/2013/347/f/d/i_don_t_want_to_ever_leave_the_lake_district_by_martinkantauskas-d6xrdch.jpg";
esperarImagen();
}
function esperarImagen(){
if (!imagen.complete){
console.log("Cargando...");
setTimeout(function(){
esperarImagen();
},16);
} else {
console.log("Completo.");
document.body.appendChild(imagen);
}
}
Thanks to user @Kaiido from stackoverwlow in English, I have solved the problem by creating a canvas with the image, since the browser expects it to finish. This is an example of a function to wait for an image (or an array of images) to load: