日期:2014-05-16  浏览次数:20733 次

定时刷新无效求大神帮助!
本帖最后由 showbo 于 2013-03-12 13:57:47 编辑
<%@ page language="java" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
request.setCharacterEncoding("gb2312");
%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%
Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(d);
out.write(dateString, 0, dateString.length());
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>AJAX定时刷新</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

<script type="text/javascript">
function test() {
var url = "index.jsp";
sendRequest(url);
}
var XMLHttpReq = false;
//创建XMLHttpRequest对象       
function createXMLHttpRequest() {
if (window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}
//发送请求函数
function sendRequest(url) {
createXMLHttpRequest();
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
var result = XMLHttpReq.responseText;
document.getElementById("data").innerHTML = result;
setTimeout("test()", 1000);
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
</script>
</head>
<body onload="test()">
<div id="data"></div>
</body>
</html>



无法每秒刷新一次,不知道哪里出了问题,请大家帮助啊.

------解决方案--------------------
var url = "index.jsp?_dc="+new Date().getDate();//防止IE下的缓存


除了这里其他没什么大问题,还有就是

    Date d = n