在呈现javascript之前预加载图像

在呈现javascript之前预加载图像,javascript,Javascript,我想在为我的平滑滑块加载所有图像时立即显示图像, 但它不起作用 var myPhotos = { _counter: 0, images: [], init: function() { ServerCall1(0, 'xml', 'rssphotos.php', function(xml) { imageObj = new Image(); $(xml).find('item').each(function(

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

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());
        });
    }

};
var myPhotos={
_柜台:0,,
图像:[],
init:function(){
ServerCall1(0,'xml','rssphotos.php',函数(xml){
imageObj=新图像();
$(xml).find('item').each(function(){
var desc=$(this.find('description').text();
var resp=getImgArray(描述);
myPhotos.images[myPhotos.\u counter]=resp[0];
我的照片。_counter++;
});
//开始预加载
对于(i=0;i
执行此函数后,我循环查看
myPhotos.images
以立即获取它们,但一个接一个的渲染速度非常慢。

可能是这样

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());
        });
    }

};
var myPhotos={
_柜台:0,,
图像:[],
init:function(){
ServerCall1(0,'xml','rssphotos.php',函数(xml){
$(xml).find('item').each(function(){
var desc=$(this.find('description').text();
var resp=getImgArray(描述);
myPhotos.images[myPhotos.\u counter]=resp[0];
我的照片。_counter++;
});
//开始预加载
对于(i=0;i
您可以非常快速地循环浏览所有图像,而不是在重新使用图像对象之前等待加载每个图像。这不起作用。谢谢,但现在我无法在jquery主函数和其他插件$(function(){alert(myPhotos.images[4].src);///整个图像数组未定义});是的,它可以在myPhotos对象everywhere和其他全局函数中访问,但在jquery函数中没有定义,我正在再次测试它;上面定义了init func,但它需要更多的时间(异步)执行,并且alert执行返回计数器0这是真的。。。这很难测试,除非在你的服务器上。谢谢你,先生,每次我使用myPhotos.images[index].src时,它都会发送获取图像的请求,你能告诉我为什么吗?因为一旦我创建了一个图像对象,为什么它会向服务器发送请求来获取它,并得到未修改的304作为响应,并且在显示图像时浪费时间。我能阻止它吗??