javascript 电子时钟 停止:单击后停止走秒; 开始:单击后开始走秒

JavaScript014

javascript 电子时钟 停止:单击后停止走秒; 开始:单击后开始走秒,第1张

经我修正后可以正常运作了

<!DOCTYPE HTML>

<html>

 <head>

  <title> </title>

<script type="text/javascript">

  var t=null

 

   function starTime()

   {

 t = setInterval(function(){

   var day=new Date()

   var h=day.getHours()

   var m=day.getMinutes()

   var s=day.getSeconds()

   h=checkTime(h)

   m=checkTime(m)

   s=checkTime(s)

   var time=h+":"+m+":"+s

   var obj= document.getElementById('time')

   obj.innerHTML=time

  },1000)

   //t=setTimeout('startTime()',1*1*1000)

   }

   function checkTime(i)

   {

   if(i<10)

   {i="0"+i}

   return i

   }

   function stopTime(){

   

 clearInterval(t)

   }

  </script>

 </head>

 <body onload="starTime()" >

<div id="time" style="text-align:center height:50px lin-height:50pxcolor:red"></div>

<input type="button" value="开始" id="start" onclick="starTime()">

<input type="button" value="暂停" id="stop" onclick="stopTime()">

 

 </body>

</html>

<html><head><script type="text/javascript">function startTime(){var today=new Date()var h=today.getHours()var m=today.getMinutes()var s=today.getSeconds()// add a zero in front of numbers<10m=checkTime(m)s=checkTime(s)document.getElementById('txt').innerHTML=h+":"+m+":"+st=setTimeout('startTime()',500)} function checkTime(i){if (i<10) {i="0" + i} return i}</script></head><body onload="startTime()"><div id="txt"></div></body></html>

setTimeout定时document.write以后,重新打开输出流,会清空页面内容,包括你以前的代码,简单改了一下:

<script>

function tick() {

var hours, minutes, seconds, xfile

var intHours, intMinutes, intSeconds

var today

today = new Date()

intHours = today.getHours()

intMinutes = today.getMinutes()

intSeconds = today.getSeconds()

if (intHours == 0) {

hours = "12:"

xfile = "午夜"

} else if (intHours <12) {

hours = intHours+":"

xfile = "上午"

} else if (intHours == 12) {

hours = "12:"

xfile = "正午"

} else {

intHours = intHours - 12

hours = intHours + ":"

xfile = "下午"

}

if (intMinutes <10) {

minutes = "0"+intMinutes+":"

} else {

minutes = intMinutes+":"

}

if (intSeconds <10) {

seconds = "0"+intSeconds+" "

} else {

seconds = intSeconds+" "

}

timeString = xfile+hours+minutes+seconds

document.getElementById("t").innerHTML=timeString

window.setTimeout("tick()", 1000)

}

window.onload=tick

</script>

<div id="t"></div>

================================

Clock.innerHTML = timeString

定时重写clock的内容,达到时钟效果