gpt4 book ai didi

php - 一段时间后自动刷新 JavaScript 功能停止工作

转载 作者:行者123 更新时间:2023-11-30 12:58:20 25 4
gpt4 key购买 nike

我需要每 5 秒刷新一次图像。无闪烁。所以搜索谷歌并找到一些解决方案。但是该代码刷新图像并且没有闪烁但它会在一段时间后停止刷新图像。有时它会在 1 分钟后停止刷新图像,有时会在 3 分钟后停止刷新图像,有时会在 15 分钟后停止刷新图像。

这是我的代码

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

<script language="JavaScript">
var x = 0,
y = 0;
var canvas, context, img;

function timedRefresh() {
canvas = document.getElementById("x");
context = canvas.getContext("2d");
img = new Image();
img.src = "CC4.png?" + Math.random();
img.onload = function () {
context.drawImage(img, x, y);
x += 0;
y += 0;
setTimeout('timedRefresh()', 5000);
};
}
window.onload = timedRefresh;

</script>
</head>

<body id="home" onload="setTimeout('timedRefresh()',5000)">
<canvas id="x" width="800" height="590"/>
</body>
</html>

最佳答案

我猜这是一个网络问题,所以它在无法加载图像时停止。尝试添加

img.onerror = function(){ setTimeout('timedRefresh()', 1000); }

所以即使出现问题它也会重试加载

关于php - 一段时间后自动刷新 JavaScript 功能停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com