啟用計時器和關閉計時器,關閉計時器時無效
<!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);
? }
*/
? var num=0;
? function startCount(){
? ? document.getElementById('count').value=num;
? ? num+=1;
? ?var 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>
//上面未注釋代碼哪里錯了,怎么無效呢?
2016-02-02
i 為局部變量 作用范圍只在startCount()函數中 ,將i定義在函數的外邊
2016-02-24
你的num+=1是什么意思。。