實現了setInterval手動開啓和自動終止功能

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>2-11</title>
 <!--   引入jQuery -->
 <link rel="stylesheet" href="default.css" type="text/css" />
<script src=".jquery.js" type="text/javascript"></script>
 <script type="text/javascript">
        var count=0;
        var a=true;
		var timeID;
		
		
        function init(){
            timeID=setInterval(function(){getJindu("nihao");},1000);//每隔1秒執行函數getJindu
			window.setTimeout('window.clearInterval(timeID)',5000); //到時間就自動停止
        }
        function getJindu(ch){
            if(a){
                document.getElementById("counter").innerHTML=count++;
            }else{
				alert(ch);
                document.getElementById("counter").innerHTML=count;
            }
        }
        function stopTimer(){
            a=false;
        }
        function startTimer(){
            a=true;
        }
		function over(){
		clearInterval(timeID);
			}
    </script>
 
    </head>
    <body style="padding-left: 6px; padding-right: 8px;" onload="init();">
    <div id="counter"></div>
    <input type="button" value="start" onclick="startTimer();"/>
	<input type="button" value="stop" onclick="stopTimer();"/>
	<input type="button" value="over" onclick="over();"/>
  </body>
</html>
實現了setInterval手動開啓和自動終止功能。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章