<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>計時器</title>
<script?type="text/javascript">
???var?i;
???function?clock(){
??????var?time=new?Date();?????????????????????
??????document.getElementById("clock").value?=?time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
???}
???//i=setInterval(clock,1000);
???function?start(){
?????i=setInterval("clock()",1000);
???}
???function?stop(){
???????clearInterval(i);
???}
</script>
</head>
<body>
??<form>
????<input?type="text"?id="clock"?size="50"??/>
????<input?type="button"?value="Start"??onclick="start()"/>
????<input?type="button"?value="Stop"??onclick="stop()"/>
??</form>
</body>
</html>
2015-10-07
一個是事件綁定機(jī)制,另一個則是點擊觸發(fā)stop()清除了定時器,clock()函數(shù)停止了每隔一秒的自動執(zhí)行。