Implement regular page refresh without screen swiping through Ajax

Source: Internet
Author: User
The home page (the page to be refreshed) requires that the system time be refreshed every minute

<HTML>

<Body onload = "makerequest (" getsystemtime. jsp ")"> // create an httprequest object

<Form name = "dateform">

<Table>

<TD colspan = "2" align = "center">
<Div id = "systimeshow"> </div> // display the platform system time
</TD>
</Table> </form>

</Body>

</Html>

Getsystemtime. jsp // obtain the current system time, that is, the page of the output result

<% @ 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 ');
}< BR >}else if (window. activexobject) {
try {
http_request = new activexobject ("msxml2.xmlhttp");
}catch (E) {
try {
http_request = new activexobject ("micro Soft. XMLHTTP ");
}catch (e) {
}< BR >}< br> If (! Http_request) {
alert ("your browser does not support the current operation. Please use IE 5.0 or later! ");
return false;
}< br>

// Define the page call method init, not Init (); No ();
Http_request.onreadystatechange = Init;
Http_request.open ('get', URL, true );

// Disable IE Cache
Http_request.setrequestheader ("If-modified-since", "0 ");

// Send data
Http_request.send (null );

// Refresh the page every 60 seconds
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 = "failed to get system time ";
}< br> document. getelementbyid ("effecimeshow "). innerhtml = "platform system time:" + result;
}else {// http_request.status! = 200
alert ("request failed! ");
}< BR >}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.