【发布时间】:2013-10-24 22:32:04
【问题描述】:
尝试让图像使用 javascript 刷新一定次数,然后停止(以避免生成大量缓存)。这段代码不起作用,所以不确定缺少什么?
<script>
var c = 0;
function fnSetTimer()
{
document.getElementById('refreshimage1').src='http://68.116.42.142:8080/cam_4.jpg?\'+new Date().getMilliseconds();
var t = setTimeout(fnSetTimer,5000);
c=c+1;
if(c>5)
clearTimeout(t);
}
</script>
<img src="http://68.116.42.142:8080/cam_4.jpg"; id="refreshimage1" onload="fnSetTimer()" width="400" />
但是,这段代码确实有效:
<img src="http://68.116.42.142:8080/cam_4.jpg"; id="refreshimage2" onload="setTimeout('document.getElementById(\'refreshimage2\').src=\'http://68.116.42.142:8080/cam_4.jpg?\'+new Date().getMilliseconds()', 5000)" width="400" />
因此,如果您将两者并排放置,则底部图像会(无限期地)刷新,但顶部图像只会加载一次并且永远不会刷新。有什么想法是我在顶部代码中遗漏的吗?
【问题讨论】:
-
你的代码中真的有
?\'+吗?将其更改为?'+
标签: javascript image timer refresh