doulin2025 2018-01-07 02:10
浏览 62
已采纳

如何使用Javascript每秒加载图像[重复]

This question already has an answer here:

I have programmed a web site in HTML/PHP/Javascript that must display the data stored in a MySQL database every second. It displays it as a graph (PNG image produced by running graph.php).

<!DOCTYPE html>

<html>
    <title>Live Tracking Run'INSA</title>
    
    <head>
        <script type = "text/javascript">
            function refresh() {
                document.getElementById('graph').src = 'graph.php';
            }
        </script>
    </head>

    <p><h2>Visualisation des données</h2></p>
    
    <body onLoad='setInterval(refresh, 1000);'>
        <img id='graph'/>
    </body>
    
</html>

visualiser.php displays the graph well, but graph.php does not update the last one as it is supposed to.

PS: The graph (made with pChart libraries) shows the heart rate as a function of time as well.

</div>
  • 写回答

1条回答 默认 最新

  • dpppic5186 2018-01-07 02:12
    关注

    Fundamentally that looks fine. I suspect the image isn't being updated because the src isn't changing when you set it the second, third, etc. times. You could either clear it before setting it:

    function refresh() {
        var graph = document.getElementById('graph');
        graph.src = '';
        graph.src = 'graph.php';
    }
    

    ...or give it an ever-changing URL by appending a query string:

    function refresh() {
        document.getElementById('graph').src = 'graph.php?' + Date.now();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部