在渲染javascript之前预加载图像

时间:2011-05-11 11:14:51

标签: javascript

我想在我的光滑滑块上加载所有图像时立即显示图像, 但它不起作用。

var myPhotos = {
    _counter: 0,
    images: [],
    init: function() {
        ServerCall1(0, 'xml', 'rssphotos.php', function(xml) {
            imageObj = new Image();
            $(xml).find('item').each(function() {
                var desc = $(this).find('description').text();
                var resp = getImgArray(desc);

                myPhotos.images[myPhotos._counter] = resp[0];
                myPhotos._counter++;

            });

            //start preloading
            for (i = 0; i < myPhotos._counter; i++) {
                imageObj.src = myPhotos.images[i];
            }

            ////PUT THE HEADER HOME PAGE
            topHeader.putData(topHeader.photoData());
        });
    }

};

执行此函数后,我循环遍历myPhotos.images以立即获取它们,但它的渲染速度非常慢。

1 个答案:

答案 0 :(得分:1)

也许这个

var myPhotos = {
    _counter: 0,
    images: [],
    init: function() {
        ServerCall1(0, 'xml', 'rssphotos.php', function(xml) {
            $(xml).find('item').each(function() {
                var desc = $(this).find('description').text();
                var resp = getImgArray(desc);

                myPhotos.images[myPhotos._counter] = resp[0];
                myPhotos._counter++;

            });

            //start preloading
            for (i = 0; i < myPhotos._counter; i++) {
                this.images[i]= new Image(); this.images[i].src = myPhotos.images[i];
            }

            ////PUT THE HEADER HOME PAGE
            topHeader.putData(topHeader.photoData());
        });
    }

};