.net动态显示当前时间(客户端javascript)
程序员文章站
2024-03-09 15:59:59
个人感觉完全为了ajax而ajax就不好了!javascript在客户端处理不是更理想吗? 复制代码 代码如下:function timer(span) { var tmp...
个人感觉完全为了ajax而ajax就不好了!javascript在客户端处理不是更理想吗?
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,date;
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();
date=timer.getdate();
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);
//同步服务器时钟
setinterval("window.location.href='当前url'", updatespan);
}
复制代码 代码如下:
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,date;
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();
date=timer.getdate();
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);
//同步服务器时钟
setinterval("window.location.href='当前url'", updatespan);
}