原文地址:http://blog.sina.com.cn/s/blog_85802e5c0100uehd.html
近日要做一个塔吊的报警系统,要求页面5s钟刷新一次,我采用的是JQuery解析XML的格式将信息读取出来,代码如下
function getWinstr(){
$.get("/XMLServlet",
{}, function(returnedData, status)
{
var flag = $(returnedData).find("isAlert").text(); //读取是否需要报警
var wintr1 = "警告 ";
if("1"==flag)
{
$(returnedData).find("user").each(function(i)
{
var CRANE_NAME = $(returnedData).find("CRANE_NAME").eq(i).text(); //从xml中解析得导数据
var ADDRESS = $(returnedData).find("ADDRESS").eq(i).text();
var ALERM_TYPE = $(returnedData).find("ALERM_TYPE").eq(i).text();
wintr1 = wintr1 + " "+CRANE_NAME+" 出现"+ALERM_TYPE+" 地址:"+ADDRESS+" ";
});
ManageSoundControl('play'); //报警声开始响起
$("#forclick").click(function(){
window.top.frames.MainFrame.location.href="1.html";
});
//$("#forclick").color("red"); 报警字体设为红色
document.getElementByIdx_x("forclick").style.color="red";
}
else { //没有报警信息
wintr1 = "吊塔运行正常 天盾电子 版权所有 长江大学研发";
ManageSoundControl('stop');
$("#forclick").click(function(){});
document.getElementByIdx_x("forclick").style.color="black";
}
$("#alert").html(wintr1);
});
var timeout = setInterval("getWinstr()",5000);
}
function ManageSoundControl(action) //控制声音播放
{
var soundControl = document.getElementByIdx_x("soundControl");
if(action == "play")
{
soundControl.play();
}
if(action == "stop")
{
soundControl.stop();
}
}
这样 问题就来了 浏览器在运行几秒之后立即卡死,虽然电脑性能有点差,可是这个问题终究得解决,经过多次试验之后得到解决办法如下;
<script language="javascript">
function detect()
{
xml = new ActiveXObject("Microsoft.XMLHTTP");
var post=" ";//构造要携带的数据
xml.open("POST","bot.jsp",false);//使用POST方法打开一个到服务器的连接,以异步方式通信
//xml.open("POST","on_line_list.jsp",false);//使用POST方法打开一个到服务器的连接,以异步方式通信
xml.setrequestheader("content-length",post.length);
xml.setrequestheader("content-type","application/x-www-form-urlencoded");
xml.send(post);//发送数据
var res = xml.responseText;//接收服务器返回的数据
list.innerHTML=res;
setTimeout("detect()",7000);//每隔5秒钟轮询一次
}
</script>
<span id="list" style="overflow: auto; height:450; width:100% " ></span>
其中bot.jsp代码中直接将查询到的数据放在页面里面,注意页面中设计到js部分加上def;
代码示例如下
while (it.hasNext())
{
Hashtable rs = (Hashtable) it.next();
str = str + Util.getNulltoStr(rs.get("CRANE_NAME"))+" 出现"+Util.getNulltoStr(rs.get("ALERM_TYPE"))+" 地址:"+Util.getNulltoStr(rs.get("ADDRESS"))+" ";
i++;
}
.......................
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" background="../../images/bot/anglebg.gif"><img src="../../images/bot/bot12.gif" width="152"
height="19"></td>
<td width="82%" onClick="window.top.frames.MainFrame.location.href='../query/real_test/real_test_query.jsp?type=alert'" align="center" valign="bottom" background="../../images/bot/anglebg.gif" style="color:#CC0000;cursor:hand;">
<span class='style2'>
<%=str%>
</span>
</td>
<td background="../../images/bot/anglebg.gif" width="3%"> <embed id="soundControl" src="1.mp3" mastersound hidden="true" loop="true" ></embed><img src="2.gif" width="20" height="20" align="left"></td>
</tr>
</table>
这样浏览器就不会卡死了,还是老办法实用