js简版定时器

本文介绍了一个简单的HTML页面,该页面使用JavaScript实现了一个基本的动态计数器功能。通过设置定时器,页面上的数字每秒自动递增,并提供了开始和结束按钮来控制计数过程。
 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 4 <title>定时器</title>
 5 <style type='text/css'>
 6 #box {width:80px; height:80px; background:#f00; position:absolute; left:50px; top:50px;}
 7 </style>
 8 <script>
 9        var $time;
10          function $(id){
11              return document.getElementById(id);
12           }
13           function start(){
14              $("span").innerHTML=parseInt($("span").innerHTML)+1;
15              $time=setTimeout('start()',1000);
16           }
17     window.onload = function() {
18          $("start").onclick=function(){
19                 start();
20              }
21              $("end").onclick=function(){
22                //取消定时器
23                 clearTimeout($time);
24                 $("span").innerHTML=1;
25              }
26       }
27 </script>
28 </head>
29 <body>
30 <span id="span">1</span><br/>
31 <input type="button" id="start" value="开始" /><br/>
32 <input type="button" id="end" value="结束" /><br/>
33 </body>
34 </html>

为制作动态时间程序作基础

转载于:https://www.cnblogs.com/tmrgd/p/5787715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值