I wrote an infinite scrolling scrolling plugin for an app I'm developing. When requesting the second 'page' from the server, I loop through each image, and give it a very basic onload
function.
// I will have the result in a variable called data
// Fetch each image from result
$images = $( data.content ).find('img');
// Cache length for use in for loop
imagesLength = $images.length;
for ( var i = 0; i < imagesLength; i++ ) {
// Create new Image object
img = new Image();
// Match src of image in the result
img.src = ( $images[i].src );
// Add onload listener
img.onload = function () {
// Increment iterator, and check if we've loaded all the images
if( ++imageLoadingIterator == images.length)
_continue(); // Run _continue() if this is the case
}
}
I feel like this isn't the best way to do this. Is there a way I can improve this function to run faster? For example: I'm thinking maybe creating a new Image
object for each img
tag is too much overhead.