這樣卻停不下來
<!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;
? ?setTimeout("startCount()",1000);
? }
? function stopCount(){
? ? clearTimeout( setTimeout("startCount()",1000));
? }
</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>
2016-05-30
? clearTimeout函數(shù)中要傳入setTimeout函數(shù)的id,把setTimeout作為一個對象傳入? clearTimeout里
可以修改成
。。。
var i = ? setTimeout("startCount()",1000);
。。。
clearTimeout(i);
。。。