简单的计时:

var t=setTimeout("alert('5 秒!')",5000)

无限计时:

var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}

实现一个暂停计时和重新开始计时的效果:

<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
} function stopCount()
{
c=0; //1
setTimeout("document.getElementById('txt').value=0",0) //2
/*若果上面2句不加就是一个暂停计时效果,加了就是重新计时效果*/
clearTimeout(t);
}
</script>
</head> <body> <form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止计时!" onClick="stopCount()">
</form> </body> </html>

实现一个时钟:

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<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
} function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
05-26 01:35