dousu8456 2017-08-18 21:40
浏览 204
已采纳

如何在setInterval页面更新时停止链接图像闪烁

I have a table that has an href link to a picture/media file. When my page updates the image [media] flashes during the setInterval function. Everything else is fine. How would I stop this from happening?

php code

<?php

include("../lib/config.php");
$conn=mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname");
    $result = mysqli_query($conn, " SELECT name, message, time, media FROM message ORDER BY ID DESC");
    mysqli_close($conn);

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['message'] . "</td>";
            echo "<td>" . $row['time'] . "</td>";
            //echo "<td>" . $row['media'] . "</td>";
            echo "<td><a href=\"" . $row['media'] . "\" target=\"_blank\" alt=\"Media not available\"> <img src=\"" . $row['media'] . "\" height=\"50\" ></a></td>";                              
        echo "</tr>";
    }
?>

js code

<script type="text/javascript">
    setInterval(function(){
        $.get("../pageLinks/messageOne.php" , function(result){
        $("#testMessageOne").html(result);
        });
     }, 20000);
</script>
  • 写回答

2条回答 默认 最新

  • doumo2501 2017-08-21 15:46
    关注

    You would need to determine that the image(s) are loaded, prior to insertion to the DOM to prevent the flicker caused as the image(s) being rendered/downloaded by the browser.

    Flicker Demo: https://jsfiddle.net/v8z5bvL2/

    To do this you would need to find the image(s) in your requested content and add a load event to trigger the insertion into the DOM once all image(s) have completed loading.

    The downside is that the content will not be updated until after the images have finished loading.

    Anti-Flicker Demo: https://jsfiddle.net/g7m1au40/

    setInterval(function(){
        $.get("../pageLinks/messageOne.php" , function(result){
            var result = $(result);
            var img = result.find('img');
            var imgCount = img.length; //count images in the content
            var imgLoaded = 0;
            img.on('load', function() {
                if (imgCount === 0 || imgLoaded++ === imgCount) {
                   //the images have finished loading, display the content.
                   $("#testMessageOne").html(result);
                }
            });
        });
    }, 20000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function