asp.net客户端同步服务端实时刷新时间

 
//首先加入下列脚本
< script >
        
//获取时间
    function Timer(span)
        
{
                            
var tmp = new Date();
            
var milsecs=Date.parse(tmp.getMonth()+"-"+tmp.getDay()+"-"+tmp.getFullYear()+" "+document.getElementById('lbl_Hour').innerText+":"+document.getElementById('lbl_Minute').innerText+":"+document.getElementById('lbl_Second').innerText);
            
var timer = new Date(milsecs+span);
            
var seconds,minutes,hours;
            
if(timer.getSeconds()<10)
                seconds 
= "0"+timer.getSeconds();
            
else
                seconds 
= timer.getSeconds();
            
if(timer.getMinutes()<10)
                minutes 
= "0"+timer.getMinutes();
            
else
                minutes 
= timer.getMinutes();
            
if(timer.getHours()<10)
                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;
        }

        
//得到服务器时间每隔updatespan分钟校验一次,每秒更新一次本地时钟
        function GetServerTime(updatespan)
        
{    
            
var clientspan = 1*1000;
            
//更新本地时钟
            setInterval("Timer("+clientspan+")",clientspan);
            
//同步服务器时钟 注意'SendErrorTaskModify.aspx'是你当前页的名称
            setInterval("window.location.href='SendErrorTaskModify.aspx'",updatespan);
            
        }

        
//-->
    
</ script >
                     //下面这个方法也可以移到aspx.cs后台文件中
    
< script  language ="C#"  runat ="server" >
        
        string GetTime(object day)
        
{
            
if(day.ToString()=="")
                
return "";
            
else
            
{
                
return(DateTime.Parse(day.ToString()).ToShortTimeString());            
            }

        
        }

    
</ script >

//接着在body 中添加如下方法
< body  onload ="GetServerTime(15*60*1000)" >

//在界面中添加如下时间的label
 
< 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 >
//在aspx.cs后台代码中PageLoad(...)中添加 这个非常重要
    lbl_Hour.Text = DateTime.Now.Hour.ToString();
        lbl_Minute.Text = DateTime.Now.Minute.ToString();
        lbl_Second.Text = DateTime.Now.Second.ToString();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值