【问题标题】:jQuery refresh div only when change happen in serverjQuery 仅在服务器发生更改时刷新 div
【发布时间】:2016-11-27 00:25:41
【问题描述】:

我正在使用 jQuery 刷新“容器”中的内容,它可以正常工作,每 9 秒刷新一次内容并从 feed.php 中获取数据并显示在“容器”中。

仅当 feed.php 发生变化时刷新“容器”的最佳做法是什么,否则显示相同的内容?

简单的html:

<body>
<ul id="container"></ul>
</body>

<script>
(function($)
{
    $(document).ready(function()
    {
        $.ajaxSetup(
        {
            cache: false,
            beforeSend: function() {
                $('#content').hide();
            },
            complete: function() {
                $('#content').show();


            },
            success: function() {
                $('#content').show();
            }
        });
        var $container = $("#content");
        $container.load("feed.php");
        var refreshId = setInterval(function()
        {
            $container.load('feed.php');
        }, 9000);
    });
})(jQuery);

</script>

【问题讨论】:

    标签: javascript php jquery


    【解决方案1】:

    为此从 php 中返回差异状态值,例如

    $response[errCode] = 0;
    $response[errCode] = 1;
    

    并在 ajax 调用中设置如下条件:

    success: function(response) {
    if(response == 0)
        {
           // refresh the container
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多