<!DOCTYPE HTML>
<html>
<head>
<title>计时器</title>
<script type="text/javascript">
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
}
var i=setInterval("clock()",100);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
<input type="button" value="Stop" onclick="clearInterval(i)" />
<input type="button" value="Start" onclick="setInterval('clock()',100)" />
</form>
</body>
</html>
<input type="button" value="Start" onclick="setInterval('clock()',100)" /> 启动按钮改成 onclick="i = setInterval('clock()',100)"
<!DOCTYPE HTML> <html> <head> <title>计时器</title> <script type="text/javascript"> function clock(){ var time=new Date(); document.getElementById("clock").value = time; } var i=setInterval("clock()",100); var timer = null; function start(){ if(timer){ clearInterval(timer); timer = null; } setInterval('clock()',100) } function stop(){ if(timer){ clearInterval(timer); timer = null; } } </script> </head> <body> <form> <input type="text" id="clock" size="50" /> <input type="button" value="Stop" onclick="start()" /> <input type="button" value="Start" onclick="stop()" /> </form> </body> </html>
因为你没有接收点按钮开启的定时的引用 那个i也不是,所以你就关不掉了