asp.net client Sync service-side Realtime Refresh Time

Source: Internet
Author: User



First, add the following script
< Script > ...
//Get Time
    functionTimer (SPAN)
...{
vartmp= NewDate ();
varMilsecs=Date.parse (Tmp.getmonth ()+"-"+Tmp.getday ()+"-"+tmp.getfullyear ()+" "+document.getElementById (''Lbl_hour''). innertext+":"+document.getElementById (''Lbl_minute''). innertext+":"+document.getElementById (''Lbl_second''). innertext);
varTimer= NewDate (Milsecs+span);
varseconds,minutes,hours;
if(Timer.getseconds ()<Ten)
Seconds
= "0"+timer.getseconds ();
Else
Seconds
=timer.getseconds ();
if(Timer.getminutes ()<Ten)
Minutes
= "0"+timer.getminutes ();
Else
Minutes
=Timer.getminutes ();
if(timer.gethours ()<Ten)
Hours
= "0"+timer.gethours ();
Else
Hours
=Timer.gethours ();
document.getElementById (
''Lbl_second''). innertext=seconds;
document.getElementById (
''Lbl_minute''). innertext=minutes;
document.getElementById (
''Lbl_hour''). innertext=Hours
}

//Get server time to check every updatespan minutes, update local clock every second
        functionGetServerTime (Updatespan)
...{
varClientspan= 1*1000;
//Update Local Clock
SetInterval ("Timer ("+Clientspan+")", Clientspan);
//Sync Server clock Note ' senderrortaskmodify.aspx ' is the name of your current page
SetInterval ("window.location.href= ' senderrortaskmodify.aspx '", Updatespan);

}

//-->
    
</ Script >
The following method can also be moved to the Aspx.cs background file
< Script language = "C #" runat = "Server" > ...

String GetTime (Object day)
...{
if(day. ToString ()=="")
return "";
Else
...{
return(DateTime.Parse, day. ToString ()). Toshorttimestring ());
}


}

</ Script >

Next, add the following method to the body
< Body onload = "GetServerTime (15*60*1000)" >

Add a label for the following time in the interface
< TR >
< TD colspan = "4" >
< Asp:label ID = "Lbl_time" runat = "Server" font-size = "X-small" ForeColor = "Blue" ></ Asp:label >
< Asp:label ID = "Lbl_hour" runat = "Server" font-size = "X-small" ForeColor = "Blue" > Label </ Asp:label > :
< Asp:label ID = "Lbl_minute" runat = "Server" font-size = "X-small" ForeColor = "Blue" > Label </ Asp:label > :
< Asp:label ID = "Lbl_second" runat = "Server" font-size = "X-small" ForeColor = "Blue" > Label </ Asp:label >
</ TD >
</ TR >
Pageload (...) in the Aspx.cs background code. It's very important to add this.
Lbl_hour.text = DateTime.Now.Hour.ToString ();
Lbl_minute.text = DateTime.Now.Minute.ToString ();
Lbl_second.text = DateTime.Now.Second.ToString ();
<

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.