如何仅在有网络连接的情况下更新屏幕/网站


How to update the screen/website only when there is a network connection?

我正在尝试为楼层信息设置一个显示屏,这是一个简单的网页。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>

可能还有其他方法可以做到这一点,但这是可行的:检查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);

您可以使用complete(jqXHR, textStatus)中的textStatus,它将是以下任意一种:"成功"、"未修改"、"错误"、"超时"、"中止"或"parserror"。

        $.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/

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