【问题标题】:Only update messages when there is a connection仅在有连接时更新消息
【发布时间】:2012-07-10 16:19:50
【问题描述】:

我构建了一个显示消息的站点,因此 msgloader.js 每 10 秒从管理员那里更新一次消息。客户端不断在屏幕上显示此站点。

问题是有时网络连接不太好,显示断开,msgloader.js 仍然更新消息,最后屏幕冻结(我知道的原因是网站上有一个时钟同样,时钟从本地机器获取时间,它只是一次冻结,直到我们刷新页面,这是一个问题)。

我怀疑这个冻结问题是因为运行的脚本太多并且机器内存已被占用。

回到问题,有什么办法可以将下面的代码设置为在有互联网连接时每 10 秒更新一次消息,否则在没有互联网连接时每隔一小时/两个更新一次消息。

感谢任何帮助。谢谢。

--------------------------------------------------------------------
Update the data for the static sections
--------------------------------------------------------------------
*/
function updateSection(sect) 
{   
    setTimeout('updateSection(' + sect + ')', 10000);

    //alert('updateSection: ' + sect);
    var ajax = new sack();
    ajax.requestFile = 'ajax/getMessages.php?section='+sect;
    ajax.method = 'post';
    /*ajax.onError = whenError;*/
    ajax.onCompletion = whenComplete;
    ajax.runAJAX();

/*  function whenError() 
    {
        alert('Could not return getMessages values. <br />Please notify the system administrator.');
    }*/

    function whenComplete() 
    {
        var messages = ajax.response;

        var messages1 = messages.split('---');

        var num_messages    = messages1[0];
        //alert('Num Lines: ' + num_messages );
        var messages_list   = messages1[1];
        //alert('MESSAGES: '+messages);
        var msg_data_array  = messages_list.split('::');

        var i=0;            
        switch(sect)
        {
            case 1:

                for(i=0;i<=num_messages;i++)
                {

                    var j = i + 1;
                    icon_to_use = 'icon'+j;

                    // Set icon class
                    var icon = document.getElementById('icon_' + sect + '_' + j);
                    icon_to_use.className = 'icon_pointer';

                    // Set message text
                    // -------------------------------------------                  
                    var msgtext_array = msg_data_array[i].split('##');


                    // Here's the title
                    // -------------------------------------------
                    var msgtext_1a = msgtext_array[1];


                    // Here's the text
                    // -------------------------------------------
                    var msgtext_1 = msgtext_array[2];

                    // Set the title space
                    // -------------------------------------------
                    var msg_1a = document.getElementById('msg_text_' + sect + '_' + j + 'a');

                    // Set the text space
                    // -------------------------------------------
                    var msg_1 = document.getElementById('msg_text_' + sect + '_' + j);

                    // Write in the title
                    // -------------------------------------------                      
                    msg_1a.innerHTML = msgtext_1a;
                     msg_1.innerHTML = "<img src='[url_of_image]' /> " + msgtext_1;
                    // Write in the text
                    // -------------------------------------------                      
                    msg_1.innerHTML = (msgtext_1) ? separator + msgtext_1 : msgtext_1;

                    //msg_1a.style.borderBottom="2px solid white";
                    msg_1a.style.borderBottom="2px solid white";
                    msg_1.style.borderBottom="2px solid white";

                }   break;
            default:
                break;          
        }

        // DEBUG
        if(debug)
        {
            debugReport
            (
                'updateSection():ID: '+msg_id+
                '<br />'+'updateSection():TIMEOUT: '+timeout+
                '<br />'+'ROTATE: '+rotate
            );
        } 
        else 
        {
            debugReset();
        }
    }
}
/*

【问题讨论】:

  • 如果您将 setTimeout 移动到完整的处理程序中,您一次只会有一个未完成的 Ajax 请求。
  • 嗨 nnn,我试图移动它,但 js 仍然每 10 秒更新一次消息。有什么办法可以设置有连接时每 10 秒更新一次,没有连接时设置一个小时?谢谢。

标签: javascript jquery ajax


【解决方案1】:

尝试使用这个,

var online = navigator.onLine;

现在你可以这样做了,

if(online){
    alert('Connection is good');
}
else{
    alert('There is no internet connection');
}

更新:

尝试将警报放在这里,

if(online){
    setTimeout('updateSection(' + sect + ')', 10000);

    //alert('updateSection: ' + sect);
    var ajax = new sack();
    ajax.requestFile = 'ajax/getMessages.php?section=1';
    ajax.method = 'post';
    /*ajax.onError = whenError;*/
    ajax.onCompletion = whenComplete;
    ajax.runAJAX();
}
else{
    alert('There is no internet connection');
}

【讨论】:

  • 我会小心使用它,这并不总是意味着你不在互联网上。见developer.mozilla.org/en/DOM/window.navigator.onLine
  • 您好,感谢您的回复。我做了 if(online){ setTimeout('updateSection(' + sect + ')', 10000); } else{ setTimeout('updateSection(' + sect + ')', 1000000); }然而,即使我断开互联网,js 仍然每 10 秒更新一次消息,我应该在哪里应用你的代码?抱歉还在学习。谢谢。嗨,DECLAN COOK,你能解释一下使用这个代码的问题吗?谢谢!
  • 把它放在你请求 AJAX 调用的地方,我没有在代码中看到你请求调用的确切位置,但在你发布的代码中,它是在 setTimeout 函数之后。
  • 您好,我已经更新了上面所有的js代码。我尝试了您更新的代码,当 Firebugs 中没有互联网连接时,我仍然可以看到 js 更新消息,并且没有警报。你能看看我的代码并给我一些建议吗?非常感谢!
【解决方案2】:

如果我对您的理解正确,您可以这样做:

每次在 ajax 请求上发生 onerror 事件时都会增加一个计数器。在连续失败的设定限制/在一定时间内失败后,您可以更改超时的长度。

var timeoutLength = 10000
setTimeout('updateSection(' + sect + ')', timeoutLength);

在 ajax 请求失败后更改 timeoutLength,IE 没有互联网连接。

编辑

var errorCount = 0;

ajax.onError = whenError;

function whenError() {
  errorCount++
  if(errorCount < 5) {
    timeoutLength = 3600000
  }
}


function whenComplete() {
   errorCount = 0
   ...
}

这需要连续出现 5 个错误才能假定互联网已关闭。你可能应该玩弄那个。但这应该向您展示总体思路。

【讨论】:

  • 嗨德克兰库克,感谢您的回复。我按照你上面的建议更改了代码,即使我断开了互联网,我仍然可以每 10 秒看到 js 在 firebugs 中运行。我正在尝试在有连接时每10秒设置一次更新部分,在没有连接时应该每小时设置一次,可以实现吗?谢谢!
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 2020-01-02
  • 2015-11-08
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多