先看下效果
话不多说上代码~
<!DOCTYPE HTML><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> <script type="text/javascript"> var num=0; var i; function clockstart(){ document.getElementById('clock').value=num; num++; i=setTimeout("clockstart()",1000); } function clockstop(){ clearTimeout(i); } function clockclear(){ num=0; setTimeout("document.getElementById('clock').value=0",0); clearTimeout(i); } </script> </head> <body> <input type="text" id="clock" /><br/><br/> <input type="button" value="开始" onclick="clockstart()" /> <input type="button" value="停止" onclick="clockstop()" /> <input type="button" value="复位" onclick="clockclear()" /> </body></html
代码
<!DOCTYPE HTML><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>秒表</title> <script type="text/javascript"> function clock(){ var time=new Date(); var attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds(); document.getElementById('clock').value=attime; } setInterval("clock()",100); </script> </head> <body> <input type="text" id="clock" /> </body></html>
原标题:setTimeout、clearTimeout、setInterval,clearInterval ——小小计时器
关键词: