代码第一次工作并且递归调用工作,但图像没有更新。注意:.src 不会更改 - 它只是在相机中每秒更新几次,因此如果我刷新页面,它会更新,但不是通过递归函数调用 - 我需要做什么才能使其更新?谢谢!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Image update from cam</title>
<script type="text/javascript">
var t;
function updateimg() {
document.getElementById('camimg').src = "urlofimgfromcamera - this is valid url";
t = setTimeout('updateimg()', 2000);
}
</script>
</head>
<body>
<img id="camimg" src="" width="1400" alt=""/>
<script type="text/javascript">
t = setTimeout('updateimg()', 2000);
</script>
</body>
</html>
最佳答案
图像未更新,因为它由您的浏览器缓存并且您使用相同的 URL。尝试向图像 URL 添加日期,您应该使用 setInterval 而不是递归调用:
var timer = setInterval(function(){
var imgUrl = "image.png?v=" + new Date().getTime();
document.getElementById('camimg').src = imgUrl
},5000);
关于javascript html update img src - 这有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222870/