I'm building a website with around 250-300 thumbnails on a single page, contained in 5 different divs which can each be scrolled horizontally.
During the loading stage, however, I need to be able to click on a thumbnail and load the full-res picture in the lightbox.
I've looked at Jason Buntings answer in How to display loading status with preloader and multiple images? which gets me there half the way: it works in IE but not in FF where it doesn't load the lightbox-image until all the thumbnails are loaded.
So I rolled my own code building on the same concept: it works but is unstable (random hangs) and uses tons of memory:
function doLoadThumbnails(queue) {
if (!queue.isEmpty()) {
if (connManager.AcquireConnection()) {
var imageLink = queue.dequeue();
var loader = new Image();
loader.onload = function() {
imageLink.firstChild.src = imageLink.href;
connManager.ReleaseConnection();
}
loader.src = imageLink.href;
doLoadThumbnails(queue);
} else {
connManager.getEventObject().bind('connReleased', function(e) {
window.setTimeout(function() {
doLoadThumbnails(queue);
}, 50);
connManager.getEventObject().unbind('connReleased', arguments.callee);
});
}
}
}
ConnectionManager looks like this:
function ConnectionManager() {
var eventObject = $('<span id="ConnectionManager"></span>').appendTo("body");
var activeConnections = 0;
var maxConnections = 5;
this.getEventObject = function() {
return eventObject;
}
this.isConnectionAvailable = function() {
return activeConnections < maxConnections;
}
this.AcquireConnection = function() {
if (activeConnections < maxConnections) {
activeConnections++;
return true;
} else {
return false;
}
}
this.ReleaseConnection = function() {
activeConnections--;
eventObject.trigger('connReleased');
}
}
Is this a basically sound concept or am I way off? Do you know any better/simpler method to do this?