ajax 导致浏览器卡死

原文地址: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>
这样浏览器就不会卡死了,还是老办法实用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值