首页 > 其他 > 详细

定时器,setInterval、clearInterval

时间:2018-06-18 17:39:35      阅读:177      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #id1{
            width: 200px;
            height: 50px;
        }
    </style>
</head>
<body>
<input type="text" id="id1" onclick="begin()"><!--事件 onclick-->
<button onclick="end()">
    停止
</button>
<script>
    function showTime() {
        var current_time=new Date().toLocaleString();//拿到当前时间且以字符串显示
        var ele=document.getElementById("id1");//拿到input的id
        ele.value=current_time;//当前时间显示在input的value中

    }
    var clock1;
    function begin() {
        if (clock1==undefined){//如果clock1==undefined
            showTime();//显示时间
            clock1 = setInterval(showTime,1000);//1秒显示一次
        }
    }

    function end() {
        clearInterval(clock1);//停止clock1
        clock1=undefined;//赋值
    }
</script>
</body>
</html>

 

定时器,setInterval、clearInterval

原文:https://www.cnblogs.com/wfl9310/p/9195575.html

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