2014-07-03 128 views
0

我正在创建一个ajax滑块来显示图像。它完美的工作,但问题是图像缓存。当图像发生变化时,由于这是ajax负载,因此此处不存在缓存,并且延迟显示图像。我如何显示图像没有任何延迟?这里是我的代码:在ajax加载缓存图像地址

JS:

$(document).ready(function(){ 
     var auto = setInterval(function(){ 
      $("#test").load("ajax.php", {count : count}, function(){ 
       count = count + 1; 
       if (count == 5) { 
        count = 0; 
       } 
      }).fadeOut(100).fadeIn(100); 
     },3000); 

PHP在ajax.php:

<?php 
    $pathArray = array("<img src='pic/85.jpg' width='128' height='169'>", 
         "<img src='pic/18.jpg' width='128' height='169'>", 
         "<img src='pic/7.jpg' width='128' height='169'>", 
         "<img src='pic/004.jpg' width='128' height='169'>", 
         "<img src='pic/26.jpg' width='128' height='169'>" 
); 

    if (isset($_POST["count"])) { 
     echo $pathArray[intval($_POST["count"])]; 
    } 
?> 

回答

0

您的浏览器应该是自动缓存图像路径无论在哪里文字是从哪里来的,无论是它的硬编码,Java脚本AJAX或任何其他,除非你另有规定。

如果有什么是缓慢的,这似乎是你的网络...

你总是可以创建一个JavaScript数组和路径保存到图片,如果它的使用计数之前被调用。

if (typeof images[count] != 'undefined') { 
    // call ajax and save the path here, 
} else { 
    // display image 
}