我正在使用Cat API,我想每5秒更换一个新图像而不刷新页面.我尝试使用setInterval交换源,但每次刷新时它都会获得相同的URL.有任何想法吗?
<div id="cats">
<img src="http://thecatapi.com/api/images/get?format=src&type=jpg&size=med" />
</div>
setInterval(function(){
$("#cats img").attr("src", "http://thecatapi.com/api/images/get?format=src&type=jpg&size=med");
},5000);
Run Code Online (Sandbox Code Playgroud)
它只是被缓存了.像这样打破缓存:
setInterval(function(){
$("#cats img").attr("src", "http://thecatapi.com/api/images/get?format=src&type=jpg&size=med&" + new Date().getTime());
},5000);
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/eqsan8z5/1/