setInterval与Ajax结合实现实时数据显示的一个怪异问题,麻烦各位进来看看并帮我分析一下,万分感谢!!!
jiagu 2007-10-16 08:28:08 有点长,请耐心看看:(
【test.html】-->作用是显示实时数据
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="JS/ajax.js"></script>
<link href='DefaultSkin.css' rel='stylesheet' type='text/css'>
<script language="javascript" type="text/javascript">
<!--
window.setInterval("getResponse();",2000);
//-->
</script>
</head>
<body>
<div id="MainIndexNewSaleRight"></div>
……//还有些内容,有链接等
</body>
</html>
【ajax.js】-->作用是完成异步获取数据
var http_request = false;
if (window.XMLHttpRequest){ // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject){ // IE
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
}
}
}
function getResponse(){
url = "Response.aspx";
http_request.open("GET",url,true);
http_request.onreadystatechange = function() {
if (!document.getElementById("MainIndexNewSaleRight")) {
return false;
}
if (http_request.readyState < 4) {
document.getElementById("MainIndexNewSaleRight").innerHTML="loading...";
}
if (http_request.readyState == 4) {
var response = http_request.responseText;
document.getElementById("MainIndexNewSaleRight").innerHTML=response;
}
}
http_request.send(null);
}
【Response.aspx】-->作用是查询数据库并返回数据,其中对应的Response.aspx.cs页中没有使用if(!this.IsPostback){}这样的语句(好像这个语句也不是影响因素)
【问题】是这样的,test.html能显示查询出的数据,但是在查询数据发生变化的时候不能实时显示,只有刷新或是点击test.html中的其它链接的时候才能看到异步显示数据的效果,如出现“loading...”,并能立刻显示出数据的变化。我想了好几天,并查看了代码,就是没弄明白为什么会出现这样的现象,还请各位经验丰富的朋友帮我看看,谢谢了!!!