English 中文(简体)
如何在有网络联系时更新屏幕/网站?
原标题: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>
最佳回答

您可使用<代码>textStatus,complete (jqXHR, textStatus),其中任何一项是:“success”、“notmodised”、“error”、“timeout”、“abort”或“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);
              }
            } 
        });

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

问题回答

也许还有其他办法可以做到这一点,但这项工作将进行:核对<代码>数据>。 其中没有任何内容:

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

In your complete: function, like so:

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

A better way to do this would be to use success: instead of complete:, as the former is only fired if the AJAX request completes successfully, meaning you don t have to check for 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);
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);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签