一个关于时间显示的问题
就是想看时间了,就单击一下,在文本框里显示时间,然后不想看了,再单击,就不显示时间了。
这个代码要怎么改才能符合上边的要求?
<html>
<head>
<script type="text/javascript">
function startTime(){
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
m = checkTime(m);
s = checkTime(s);
calculate.screen.value = h + ":" + m + ":" + s;
setTimeout('startTime()',500);
}
function checkTime(i){
if (i<10){
i="0" + i;
}
return i;
}
</script>
</head>
<body >
<form name = "calculate" action = "">
<input name = "showTime" type = "button" value = " time " onclick = "startTime()" />
<input name = "screen" type = "text" size = "32" value = "0">
</body>
</html>
------解决方案--------------------
HTML code
<script type="text/javascript">
var tid;
var b = true;
function startTime(){
if(b) return;
clearTimeout(tid);
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
m = m<10?"0"+m:m;
s = s<10?"0"+s:s;
document.calculate.screen.value = h + ":" + m + ":" + s;
tid = setTimeout('startTime()',1000);
}
</script>
</head>
<body >
<form name = "calculate" action = "">
<input name = "showTime" type = "button" value = " time " onclick = "b=!b;startTime()" />
<input name = "screen" type = "text" size = "32" value = "0">
</form>
</body>
</html>