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.