Automatic page time update and automatic page update

Source: Internet
Author: User

Automatic page time update and automatic page update

1. Get the Time of the local machine through JavaScript and update it automatically

<Script>
Function displayTime (){
Var date = new Date (); // date object
Var now = "";
Now = date. getFullYear () + "year ";
Now = now + (date. getMonth () + 1) + "month ";
Now = now + date. getDate () + "day ";
Now = now + date. getHours () + "Hour ";
Now = now + date. getMinutes () + "Minute ";
Now = now + date. getSeconds () + "seconds ";
Document. getElementById ("getToday"). innerHTML = now;
SetTimeout ("displayTime ()", 1000 );
}
Window. onload = displayTime;
</Script>

<Div id = "getToday">
</Div>

TIPS: You can use JavaScript to automatically update the year displayed at the bottom of the page.

<Script type = "text/javascript">
Copyright = new Date (); // get the current Date
Update = copyright. getFullYear (); // get the current year
Document. write ("Copyright & copy; 2015-" + update + "All rights reserved .");
</Script>

2. Get the server time through AJAX and update it automatically on the page

<Script type = "text/javascript" src = "Scripts/jquery-1.10.2.js"> </script>
<Script type = "text/javascript">
$ (Function (){
SetInterval (function (){
$. Get ("GetTime. aspx? Timestamp = "+ new Date (). getTime (), function (data ){
$ ("# Time" pai.html (data );
});
},1000 );
})
</Script>

<Div id = "time">
</Div>

GetTime. aspx background code:

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{

Response. Write (DateTime. Now. ToString ());
}
}

If only the time is displayed, you can use the 1st method, that is, you can use JavaScript to obtain the local time and update it automatically.

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.