高手能给详细解释解释一个JAVAScript吗,我分不多只能给30.

来源:百度知道 编辑:UC知道 时间:2024/05/09 21:00:59
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>网页特效观止|JsCode.CN|---年月日时分秒的即时显示</title>

</head>

<body onload=startclock()>

<form name="clock">
<script type="text/javascript">
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function startclock () {
stopclock();
showtime();}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = now.getYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日" +((hours >= 12) ? " 下午 " : " 上午 " )
timeValue += ((hours >12) ? hours -12 :hours)
timeValue +=

这段javascript有三个函数我想你也应该看出来了,stopclock,startclock和showtime,其实如果想实现这个的效果stopclock,startclock这两个是没有用的,删掉也没有关系。最主要的是showtime这个函数,
1:var now = new Date();
2:var hours = now.getHours();
3:var minutes = now.getMinutes();
4:var seconds = now.getSeconds()
5:var timeValue = now.getYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日" +((hours >= 12) ? " 下午 " : " 上午 " )
6:timeValue += ((hours >12) ? hours -12 :hours)
7:timeValue += ((minutes < 10) ? ":0" : ":") + minutes
8:timeValue += ((seconds < 10) ? ":0" : ":") + seconds
9:document.clock.thetime.value = timeValue;
10:timerID = setTimeout("showtime()",1000);

我把上面的代码加上行号,这样方便解释,呵呵。
第1行是声明当前时间的对象now
第2行得到当前的小时
第3行得到当前分钟
第4行得到当前秒
第5行组成想要显示在页面上的时间字符串timeValue中的年月日,((hours >= 12) ? " 下午 " : " 上午 " )这个的意思是如果小时大于12则显示下午,否则