首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

一个关于时间显示的有关问题

2012-03-11 
一个关于时间显示的问题就是想看时间了,就单击一下,在文本框里显示时间,然后不想看了,再单击,就不显示时间

一个关于时间显示的问题
就是想看时间了,就单击一下,在文本框里显示时间,然后不想看了,再单击,就不显示时间了。
这个代码要怎么改才能符合上边的要求?

<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> 

热点排行