3

I'm just want to check for the remote image every 5 secconds, but can't make it work.

<script>
var img = new Image();
var url = "http://192.168.20.99/led/intro2.gif";

img.onload = function(){
    // display image or whatever you need
};
img.onerror = function(){
    alert("HOT BERRY IS NOT CONNECTED");  
    // handle the error
}
img.src = url;
 5000;
</script>
  • Possible duplicate of [image.onError event never fires, but image isn't valid data - need a work around](https://stackoverflow.com/questions/9809015/image-onerror-event-never-fires-but-image-isnt-valid-data-need-a-work-around) – sancoLgates Sep 06 '19 at 03:34

2 Answers2

2

You can use setTimeout/setInterval for your requirement.

setTimeout(function() {
   $('#test').attr("src", url);
}, 5000);

var img = new Image();
var url = "http://a9.vietbao.vn/images/vn899/55/2018/04/20180405-nu-sinh-sai-thanh-gay-sot-vi-giong-hot-girl-dep-nhat-han-quoc-1.jpg";

img.onload = function(){
    // display image or whatever you need
    setTimeout(function() {
         $('#test').attr("src", url);
      }, 5000);
};
img.onerror = function(){
    alert("HOT BERRY IS NOT CONNECTED");  
    // handle the error
}
img.src = url;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id='test' width="200px"/>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

This is the actual solution to my own question.

<script>
    function imgLoad(){
        var img = new Image();
        var url = "http://192.168.20.99/led/intro2.gif";

        img.onload = function(){
            // display image or whatever you need
        };
        img.onerror = function(){
            alert("HOT BERRY IS NOT CONNECTED");  
            // handle the error
        }
        img.src = url;
    }
    imgLoad();
    var interval = setInterval(function() {
        imgLoad();
    }, 5000);
</script>