Mr丶Jia
2017-01-03 18:54
<!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();
document.getElementById("clock").value = time;
}
var i=setInterval("clock()",100);
function za(){
var j=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="za()"/>
</form>
</body>
</html>
你定义的变量是局部变量,这样每次都会创建新的计时器,而你只关闭一个计时器,所以第二次会看似无效,我把代码改一下你看看吧
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> <script type="text/javascript"> var i; function clock() { var time = new Date(); document.getElementById("clock").value = time; } i = setInterval("clock()", 100); function za() { 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="za()" /> </form> </body> </html>
你也解决了我的问题!! 再次感谢
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题