码迷,mamicode.com
首页 > Web开发 > 详细

js中定时器的使用

时间:2016-05-13 13:25:48      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

1.setInterval


<!DOCTYPE html>
<html>
<head>
    <title>json</title>
<script type="text/javascript">
    function show(){
        alert("a");
    }
    //每隔1s执行一次函数
    setInterval(show, 1000);
</script>
</head>
<body>
</body>
</html>

2.setTimeout


 

<!DOCTYPE html>
<html>
<head>
    <title>json</title>
<script type="text/javascript">
    function show(){
        alert("a");
    }
    //1s之后执行该函数且只执行一次
    setTimeout(show, 1000);
</script>
</head>
<body>
</body>
</html>

2.clearInterval


<!DOCTYPE html>
<html>
<head>
    <title>json</title>

</head>
<body>
    <button type="button" id="open">开启</button>
    <button type="button" id="close">关闭</button>
    <script type="text/javascript">
    function show(){
        alert("a");
    }
    var oBtn1 = document.getElementById(open);
    var oBtn2 = document.getElementById(close);
    var timer = null;
    oBtn1.onclick=function (){
        timer = setInterval(show, 1000);
    }
    oBtn2.onclick = function (){
        clearInterval(timer);
    }
</script>
</body>
</html>

 

 

 

js中定时器的使用

标签:

原文地址:http://www.cnblogs.com/xidongyu/p/5487643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!