对于我的网站,我需要重新绘制服务器上不断更新的图片。我找到了解决方案并尝试了,但仍然没有自动刷新。我也尝试添加一个虚拟字符串到我的图像(local0.jpg),以便浏览器不使用缓存的图像,但没有成功。我究竟做错了什么?自动刷新HTML画布图片不起作用
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<canvas id="canvas" width="400" height="400"> </canvas>
<script>
var imageObj=new Image();
imageObj.src="local0.jpg?dummy=8484744"
imageObj.onload=function()
{
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.drawImage(imageObj,0, 0,canvas.width,canvas.height);
setTimeout(timedRefresh,1000);
}
function timedRefresh()
{
imageObj.src = "local0.jpg?dummy=8484749"
}
</script>
</body>
</html>
您需要您设置源 – mplungjan