Ajax在不刷屏的前提下實現頁面定時重新整理

來源:互聯網
上載者:User

首頁面(要求重新整理的頁面)要求每一分鐘重新整理一次系統時間

<html>
<body onload="makeRequest("getSystemTime.jsp")"> //建立httpRequest對象
<form name="dateForm">
<table>
<td colspan="2" align="center">
     <div id="sysTimeShow"></div> //顯示平台系統時間
    </td>
</table></form>
</body>
</html>
getSystemTime.jsp   //獲得系統的目前時間,也就是輸出結果的頁面
<%@ page language="java" %>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.Date"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="java.io.IOException"%>
<%
Date d = new Date();
Timestamp ts = new Timestamp(d.getTime());
String result = ts.toString().substring(0,16);
out.write(result,0,result.length());
%>

ajax.js

function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) {
 http_request = new XMLHttpRequest();
 if (http_request.overrideMimeType){
  http_request.overrideMimeType('text/xml');
 }
} else if (window.ActiveXObject) {
 try{
  http_request = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
  http_request = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) {
  }
 }
}
if (!http_request) {
 alert("您的瀏覽器不支援當前操作,請使用 IE 5.0 以上版本!");
 return false;
}
//定義頁面調用的方法init,不是init();沒有();
http_request.onreadystatechange = init;
http_request.open('GET', url, true);
//禁止IE緩衝
http_request.setRequestHeader("If-Modified-Since","0");
//發送資料
http_request.send(null);
//每60秒重新整理一次頁面
setTimeout("makeRequest('"+url+"')", 60000);
}
function init() {
if (http_request.readyState == 4) {
 if (http_request.status == 0 || http_request.status == 200) {
  var result = http_request.responseText;
  if(result==""){
  result = "系統時間擷取失敗";
  }
  document.getElementById ("sysTimeShow").innerHTML="平台系統時間:"+result;
 } else {//http_request.status != 200
  alert("請求失敗!");
 }
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.