2011-05-11 106 views
0

我想立即显示图像,因为所有的图像加载为我的平滑滑块, ,但它不工作。预渲染图像之前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让他们瞬间,但其呈现一个个非常缓慢。

+0

你很快通过所有图片循环,而不是等待每个加载您重新使用的图像对象之前。这是行不通的。 – mplungjan 2011-05-11 11:25:29

回答

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

}; 
+0

感谢,但现在我不能访问它的jquery主要功能和其他插件内\t $(函数() \t { \t \t警报(myPhotos.images [4]的.src); ///整个图像的阵列是undefined }); – MZH 2011-05-12 11:30:24

+0

你可以试试我的更新中的this.images吗? – mplungjan 2011-05-12 11:32:39

+0

是的它可以在myPhotos对象内部和其他全局函数内部访问,但是在jquery里面有未定义的函数,我再次测试它。 – MZH 2011-05-12 12:21:00