预加载图像时Internet Explorer循环错误

时间:2010-05-24 12:02:56

标签: javascript internet-explorer

我创建了一个JavaScript应用程序,它要求首先预先加载所有图像。在Firefox中一切正常,但在Internet Explorer中,我的循环跳过计数为19并转到21.有没有人遇到此问题之前是什么原因引起的?

您可以复制并粘贴以下脚本以进行测试。

var preLoad = function () {
    var docImages = ["http://www.sanatural.co.za/media/images/map/rsa_prov.gif", "http://www.sanatural.co.za/media/images/map/loading.gif", "http://www.sanatural.co.za/media/images/map/loading2.gif", "http://www.sanatural.co.za/media/images/map/ec_land.gif", "http://www.sanatural.co.za/media/images/map/ec_roll.gif", "http://www.sanatural.co.za/media/images/map/ec_state.gif", "http://www.sanatural.co.za/media/images/map/fs_land.gif", "http://www.sanatural.co.za/media/images/map/fs_roll.gif", "http://www.sanatural.co.za/media/images/map/fs_state.gif", "http://www.sanatural.co.za/media/images/map/gt_land.gif", "http://www.sanatural.co.za/media/images/map/gt_roll.gif", "http://www.sanatural.co.za/media/images/map/gt_state.gif", "http://www.sanatural.co.za/media/images/map/kzn_land.gif", "http://www.sanatural.co.za/media/images/map/kzn_roll.gif", "http://www.sanatural.co.za/media/images/map/kzn_state.gif", "http://www.sanatural.co.za/media/images/map/lp_land.gif", "http://www.sanatural.co.za/media/images/map/lp_roll.gif", "http://www.sanatural.co.za/media/images/map/lp_state.gif", "http://www.sanatural.co.za/media/images/map/mp_land.gif", "http://www.sanatural.co.za/media/images/map/mp_roll.gif", "mp_state.gif", "http://www.sanatural.co.za/media/images/map/nc_land.gif", "http://www.sanatural.co.za/media/images/map/nc_roll.gif", "http://www.sanatural.co.za/media/images/map/nc_state.gif", "http://www.sanatural.co.za/media/images/map/nw_land.gif", "http://www.sanatural.co.za/media/images/map/nw_roll.gif", "http://www.sanatural.co.za/media/images/map/nw_state.gif", "http://www.sanatural.co.za/media/images/map/wc_land.gif", "http://www.sanatural.co.za/media/images/map/wc_roll.gif", "http://www.sanatural.co.za/media/images/map/wc_state.gif"],
        imageFolder = [],
        loaded = [],
        loadedCounter = 0;

    this.loadImgs = function () {
        for (var i = 0; i < docImages.length; i++) {
            imageFolder[i] = new Image();
            imageFolder[i].src = docImages[i];
            loaded[i] = false;
        }
        intervalId = setInterval(loadedCheck, 10); //  
    };

    function loadedCheck() {
        if (loadedCounter == imageFolder.length) { // all images have been preloaded 
            clearInterval(intervalId);
            alert('All images have been preloaded!');
            return;
        }

        for (var i = 0; i < imageFolder.length; i++) {
            if (loaded[i] === false && imageFolder[i].complete) {
                loaded[i] = true;
                loadedCounter++;
                alert(i); // (work fine in FF but i.e goes from 19 to 21 )
            }
        }
    }
};


var preloadObject = new preLoad();

preloadObject.loadImgs();

1 个答案:

答案 0 :(得分:3)

原因是索引20上的项目是"mp_state.gif" - 它缺少完整的网址。 因此,未加载图像,.complete属性设置为false,因此循环跳过此项。