【问题标题】:How to update the screen/website only when there is a network connection?仅在有网络连接时如何更新屏幕/网站?
【发布时间】:2011-12-04 02:25:33
【问题描述】:

我正在尝试为楼层信息设置一个显示屏幕,这是一个简单的网页。 ajax 调用用于根据客户端用于更新信息的管理页面每 10 秒更新一次屏幕。

我的问题是当没有互联网连接时,显示仍然会更新并且什么都不显示。有什么办法可以改变下面的代码,如果有互联网连接则更新数据库,如果没有网络连接则重置计时器并且什么都不做。

    <script type="text/javascript">

        // Run the AJAX call to grab the data once
        $.ajax({
                type: "POST",       
                url: "ajax/getMessages.php?section=1", 
                data: "",
                 complete: function(data){  
                //print result in targetDiv  
                $('#content_1').html(data.responseText);  
                } 
            });

        // Then run the same script on a 10-second timer
        setInterval(function(){

            $.ajax({
                type: "POST",   
                url: "ajax/getMessages.php?section=1", 
                data: "",
                 complete: function(data){  
                //print result in targetDiv  
                $('#content_1').html(data.responseText);  
                } 
            });

        },10000);

    </script>

【问题讨论】:

    标签: php javascript ajax


    【解决方案1】:

    可能还有其他方法可以做到这一点,但这会起作用:检查data.responseText 中是否有任何内容:

    if(data.responseText.length)
    {
        // Display page
    }
    

    在你的 complete: 函数中,像这样:

    complete: function(data){ 
        if(data.responseText)
        {
            $('#content_1').html(data.responseText);  
        }
    } 
    

    更好的方法是使用success: 而不是complete:,因为前者仅在AJAX 请求成功完成时才被触发,这意味着您不必检查data.length

    // Run the AJAX call to grab the data once
    $.ajax({
        type: "POST",      
        url: "ajax/getMessages.php?section=1", 
        data: "",
        success: function(data) {  
            // Print result in targetDiv  
            $('#content_1').html(data.responseText);  
        },
        error: function() {
            // Error handling code
        }
    });
    
    // Then run the same script on a 10-second timer
    setInterval(function() {
        $.ajax({
            type: "POST",      
            url: "ajax/getMessages.php?section=1", 
            data: "",
            success: function(data) {  
                // Print result in targetDiv  
                $('#content_1').html(data.responseText);  
            },
            error: function() {
                // Error handling code
            }
        });
    }, 10000);
    

    【讨论】:

    • 嗨 JamWaffles,我是新手,我应该把你的代码放在哪里?如果你能用我上面现有的代码给我看,那就太好了。感谢您的帮助。
    • 请查看我的编辑。您需要将 if() 添加到您的 complete: 函数中。
    • 您好 JamWaffles,非常感谢您的详细解释。我把 if(data.responseText) 放进去,它起作用了,但是当我尝试你的第二个建议时,我将完整的功能更改为成功,显示完全消失,我不确定为什么?使用 if(data.responseText) 有什么缺点?我也尝试了 Hakre 的代码,看起来还可以。非常感谢您的帮助!
    • 我不太清楚为什么它不起作用。当然,使用 if() 很好。
    【解决方案2】:

    您可以使用来自complete(jqXHR, textStatus)textStatus,它可以是以下任意一种:“success”、“notmodified”、“error”、“timeout”、“abort”或“parsererror”。

            $.ajax({
                type: "POST",   
                url: "ajax/getMessages.php?section=1", 
                data: "",
                complete: function(data, textStatus)
                {
                  //print result in targetDiv if successfull
                  if (textStatus == 'success')
                  {
                      $('#content_1').html( + ': ' + data.responseText);
                  }
                } 
            });
    

    http://api.jquery.com/jQuery.ajax/

    【讨论】:

    • 嗨 Hakre,我是 ajax 的新手,你能在我的代码中给我一些例子吗?非常感谢。同时,我将尝试通过您的链接学习。谢谢!
    • @SamIAm:我已在我的回答中将textStatus 添加到您的示例功能代码中。这应该会给你一些解决问题的建议。
    • 您好,Hakre,非常感谢您的帮助。我将 complete: function(data, textStatus) { if (textStatus == 'success') 添加到更新 10 秒代码的第二部分,它不起作用,但是当我将代码放在我的 ajax 调用的两个部分中时这是抓取数据并更新 10 秒,它似乎正在工作。我必须保持两个部分的恒定性吗?非常感谢!很高兴看到它工作:)
    • @SamIAm:当然,您需要将其放入两个回调中。另一种选择是编写一个回调函数并将其分配给两个事件。或者你甚至用一个函数替换重复的代码,这样你就没有重复代码。不要重复代码,始终防止重复代码。更少的代码,更少的调试内容。更容易编码:)
    【解决方案3】:
    var timer = setInterval(function(){
            $.ajax({
                type: "POST",                           
                url: "ajax/getMessages.php?section=1", 
                data: "",
                success: function(data){  
                 //print result in targetDiv  
                 $('#content_1').html(data.responseText);  
                },
                error: function(){ clearInterval( timer ) } 
            });
    
        },10000);
    

    【讨论】:

    • 您好 Interstellar_Coder,非常感谢您的回复。我试过你的代码,但没有用。没有连接时,楼层信息仍然消失。还有其他建议吗?我是新来的,所以任何帮助表示赞赏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2017-07-26
    • 1970-01-01
    相关资源
    最近更新 更多