Follow on from:
Javascript wait for image to load before calling Ajax
function initResources() {
var newImage;
for (i = 0; i < resourceData.length; i++) {
// Create the image
newImage = $('<img alt="Big" id="imgA' + i + '" class="mediaImg" width="' + Math.round(resourceData[i][2] * currentScale) + '" height="' + Math.round(resourceData[i][3] * currentScale) + '" />');
newImage.load(function() {
alert(i);
// Position
$('#imgA' + i).position().top(Math.round(resourceData[i][5] * currentScale));
$('#imgA' + i).position().left(Math.round(resourceData[i][4] * currentScale));
});
newImage[0].src = uploadFolder + '/' + imgData[resourceData[i][1]][1];
$('#thePage').append(newImage);
}
}
I have an array of images. When the page is loaded initially from the server, this function loops through all the images and places them on the page.
This function is also called when the scale of the page changes. The scaling function clears the HTML and redraws everything with the scale multiplier.
The problem with this function, is that it always alerts resourceData.length, which is the end of the loop. I need to somehow pass data to the load function, so that when the image does finally load, it is referencing the correct ID/i.