Home  >  Article  >  Web Front-end  >  如何解决因为缓存无法及时更新图片问题_html/css_WEB-ITnose

如何解决因为缓存无法及时更新图片问题_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:31:212228browse

如何解决因为缓存无法及时更新图片问题:
在加载远程图片的时候,往往会由于缓存的原因,即使是在服务器更新了图片有时候也得不到及时的更新。可以使用如下方法解决此问题,代码如下:

 

$(".myImg").src("yourUrl.action?randUrl"+Math.ceil(new Date()/3600000);

 

实现的原理很简单,就是让每次加载的图片的src属性不同就可以做到了,这个时候可能很多人认为每次图片的src属性不同的话,图片不久乱了吗,其实不然,看一个例子:

<img  src="1.jpg?1231asd" alt="如何解决因为缓存无法及时更新图片问题_html/css_WEB-ITnose" >

上面的代码中,尽管后面添加了其他参数,但是加载的依然i1.jpg,如果每次后更新后面的参数,那么每次都会加载最新的图片。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=7958

更多内容可以参阅:http://www.softwhy.com/jquery/

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn