Javascript实现计时和停止计时

来源:互联网 发布:雅思培训机构知乎 编辑:程序博客网 时间:2024/06/06 10:01
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>计时器</title>
        
        <script type="text/javascript">
        var sec = 0;
        //设置计时器
        var timeId = setInterval("count();",1000);
       
        //计数
        function count(){
        document.getElementById("num").innerHTML=sec++;
       
        }


            //停止计时器
            function stopTime(){
            clearInterval(timeId);
            }
        </script>
    </head>
    <body>
        <h1>计时器</h1>
                     计时中<font id="num" color="red" >0</font><input type="button" value="停止" onclick="stopTime();"/>
    </body>
</html>
原创粉丝点击