javascript - 每 5 秒在后台检查一次远程图像

标签 javascript jquery

我只想每 5 秒检查一次远程图像,但无法使其工作。

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

最佳答案

您可以根据您的要求使用setTimeout/setInterval

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

关于javascript - 每 5 秒在后台检查一次远程图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815337/

相关文章:

javascript - 在中间而不是末端包裹一排 block (CSS/JS/jQ)

Javascript 类传递参数

jquery - 评估传递给 :onclick in Rails 的 jQuery 语句中的 Ruby 变量

javascript - Youtube,该网站本身卡住了几秒钟

javascript - 问题: The whole page loading instead just the div with .加载()

javascript - Azure Blob 存储 403 身份验证由于授权 header 而失败

javascript - 在 solr 查询中返回空字段

javascript - jQuery 或 JS : Piggyback on post return?

jquery - 如何向幻灯片添加下一个/上一个按钮

javascript - 在脚本rails haml文件中嵌入ruby,如何?