简体   繁体   中英

Auto refresh JavaScript function stop working after some time

i need to refresh image in every 5 sec. without flicker. so search google and find some solution. but that Code refresh image and without flicker but it stop refresh image after some time. some time its stop refresh image after 1 min, some time after 3 min some time after 15 min.

Here is my code

<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>

I guess it is a network issue so it stops when it can't load the image. Try adding

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

so it retries the load even if there was an issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM