<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>定时器</title> <script type="text/javascript"> var i; function start(){ var time=new Date(); var attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); document.getElementById('clock').value = attime; i = setInterval(start,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>
定时器移出start函数
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>定时器</title> <script type="text/javascript"> var i; function start(){ var time=new Date(); var attime= time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); document.getElementById('clock').value = attime; } i = setInterval(start,1000); </script> </head> <body> <form> <input type="text" id="clock" size="50" /> <input type="button" value="Start" onclick="setInterval(start,1000);"> < input type="button" value = "Stop" onclick = "clearInterval(i)" /> </form> </body> </html>