Return to Snippet

Revision: 52922
at November 6, 2011 03:03 by alik


Initial Code
function displayElem(elem) {
    if(document.getElementById(elem) != null) {
        document.getElementById(elem).style.display = 'block';
    } else {
        window.setTimeout(function() {
            displayElem(elem);
        }, 80);
    }
}

function preloadSuzukies(suz_obj) {
    var smatch = suz_obj.constructor.toString().match(/([^\(\)\s]+)\(\)/);
    switch(smatch[1]) {
        case "String":
            objImg = new Image();
            objImg.src = suz_obj;
            break;
        case "Object":
            var objArray = [];
            for(var i in suz_obj) {
                objArray.push(new Image());
                objArray[objArray.length - 1].src = suz_obj[i][0];
                if(typeof suzuki_imgs[i][1] !== "undefined") {
                    objArray[objArray.length - 1].onload = (function(iter) {
                        displayElem(suz_obj[iter][1]);
                    })(i);
                }
            }
            break;
        case "Array":
            for(var i = 0; i < suz_obj.length; i++) {
                objImg = new Image();
                objImg.src = suz_obj[i];
            }
            break;
        default:
            alert("wrong argument is given for image preloading");
            break;
    }
}
var suzuki_imgs = {
    img1: ['images/suzuki_car.png', 'car_bg'],
    img2: ['images/get_spec_hover.png']
};
preloadSuzukies(suzuki_imgs);

Initial URL


Initial Description
wrote this snippet as a necessity to preload many images without writing same code all the time in JS...

Initial Title
JS mass image preloading

Initial Tags
javascript, image, images

Initial Language
JavaScript