首页 > 编程语言 > 详细

JavaScript - 停止计时器

时间:2015-04-25 18:28:35      阅读:196      评论:0      收藏:0      [点我收藏+]

停止计时器

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>停止计时器</title>
    <script type="text/javascript">
        //页面加载完成后执行
        window.onload=function(){
            var today=new Date();
            //var date1=new Date("2014/02/14 12:30:00");
            //alert(date1);
            var year=today.getFullYear();
            var month=today.getMonth()+1;
            var date=today.getDate();
            var hour=today.getHours();
            hour=checkTime(hour);
            var minute=today.getMinutes();
            minute=checkTime(minute);
            var second=today.getSeconds();
            second=checkTime(second);
            var currentTime=year+"年"+month+"月"+date+"日 "+hour+":"+minute+":"+second;

            document.getElementById("time").innerHTML=currentTime;

            //print();
        }

        //检查时间,为个位数补0
        function checkTime(t){
            if(t<10){
                t="0"+t;
            }
            return t;
        }

        //定时器setTimeout()
        var timer;
        function print(){
            var objP=document.getElementById("itany");
            objP.innerHTML+="Hello World!<br/>";
            timer=setTimeout("print()",500);
        }

        //停止计时器
        function stopTimer(){
            clearTimeout(timer);
        }

        //定时器setInterval()
        function print2(){
            var objP=document.getElementById("itany");
            objP.innerHTML+="Hello World!<br/>";
        }

        var timer2;
        function startTimer2(){
            timer2=setInterval("print2()",1000);
        }

        function stopTimer2(){
            clearInterval(timer2);
        }
    </script>
  </head>

  <body>
    <div id="time">  </div>
    <input type="button" value="启动计时器" onclick="startTimer2()" />&nbsp;<input type="button" value="停止计时器" onclick="stopTimer2()" />
    <p id="itany"></p>
  </body>
</html>

JavaScript - 停止计时器

原文:http://blog.csdn.net/wangzi11322/article/details/45270695

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!