<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>计时器</title> </head> <script type="text/javascript"> var num=0; var i; function startCount(){ document.getElementById('count').value=num; num=num+1; i=setTimeout("startCount()",1000); } function stopCount(){ clearTimeout(i); } </script> </head> <body> <form> <input type="text" id="count" /> <input type="button" value="Start" onClick("startCount()") /> <input type="button" value="Stop" onClick("stopCount()") /> </form> </body> </html>
<input type="button" value="Start" onClick("startCount()") /> <input type="button" value="Stop" onClick("stopCount()") /> <!--这里onclick后面不是用“=”吗?这括号目前我是没见过有用过-->
每点一次start按钮,就会调用一次startCount()函数,所以计时速度就会加快。
如果想停止就,点相应次数的stop。
function startCount(){
clearTimeout(i);
document.getElementById('count').value=num;
num=num+1;
i=setTimeout("startCount()",1000);
}
主要的原因是你在start里面没有清除原来的定时器, 如果点击多次之后, stop也需要点击多次才会停止所有已开启的定时器
大概看了下,本身逻辑没有问题,把下面的点击事件后面改成"="试试
<input type="button" value="Start" onClick="startCount()" />
<input type="button" value="Stop" onClick="stopCount()" />
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
</head>
<script type="text/javascript">
var num=0;
function startCount(){
document.getElementById('count').value=num;
num=num+1;
i=setTimeout(startCount,1000);
}
setTimeout(startCount,1000);
function stopCount(){
clearTimeout(i);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="Start" onClick("startCount()") />
<input type="button" value="Stop" onClick("stopCount()") />
</form>
</body>
</html>
<script type="text/javascript">
var num=0;
var i;
function startCount(){
document.getElementById('count').value=num;
for(i=0;i<num.length;i++) {
num=num+1;
i=setTimeout("startCount()",1000);
}
}
function stopCount(){
clearTimeout(i);
}
</script>
</head>
<body>
<form>
<input type="text" id="count" />
<input type="button" value="Start" onClick("startCount()") />
<input type="button" value="Stop" onClick("stopCount()") />
</form>
</body>
</html>