运行结果如图:

点击开始计时,就会显示系统时间,然后这个时钟会每秒更新,点击停止计时,这个时间就会停止。这个是简易版本。
代码如下:
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <metacharset="UTF-8">
5 <title>Title</title>
6 </head>
7 <br>
8 <inputtype="button" value="开始计时" onclick="startTime()"></br>
9 <script type="text/javascript">
10
11 functionshowTime(){
12 vard=new Date();
13 varyea=d.getFullYear();
14 varmonth=d.getMonth();
15 vardat=d.getDate();
16 varhour=d.getHours();
17 varminutes=d.getMinutes();
18 varseconds=d.getSeconds();
19 document.getElementById("001").innerHTML=yea+"年"+(month+1)+"月"+dat+"日"+hour+"时"+minutes+"分"+seconds+"秒";
20 }
21
22 vara;
23 functionstartTime(){
24 a=setInterval("showTime()",1000);
25 }
26 functionstopTime(){
27 clearInterval(a);
28 }
29 </script>
30 <inputtype="button" value="停止计时" onclick="stopTime()"></br>
31 <span>现在时间是:</span>
32 <span id="001"></span>
33 </body>
34 </html>
本文介绍了一个使用JavaScript编写的简易计时器。当点击开始计时按钮时,页面会显示并每秒更新系统时间;点击停止计时,时间更新将停止。代码包括开始、停止计时的函数及时间显示的逻辑。

301

被折叠的 条评论
为什么被折叠?



