This example describes a method for invoking a callback function after a JavaScript control picture is loaded. Share to everyone for your reference. The specific analysis is as follows:
This code can control the execution of the specified callback function after the picture loading is complete in the specified range.
Copy Code code as follows:
function when_images_loaded ($img _container, callback) {
/* Do callback the images in $img _container (JQuery object) are loaded. _container are newly inserted images and this function are called after image images s are inserted into the target. */
var _imgs = $img _container.find (' img '),
Img_length = _imgs.length,
img_load_cntr = 0;
if (img_length) {//if the $img _container contains new images.
_imgs.on (' Load ', function () {//then We avoid the callback until images are loaded
img_load_cntr++;
if (img_load_cntr = = img_length) {
Callback ();
}
});
}
else {//otherwise Just does the main callback action if there ' no images in $img _container.
Callback ();
}
}
I hope this article will help you with your JavaScript programming.