【发布时间】: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