美文网首页
dom_8 定时器

dom_8 定时器

作者: basicGeek | 来源:发表于2018-11-23 14:49 被阅读0次
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>

    //setInterval();   setTimeout();
//    var num = 0;
//    setInterval(function () {
//        console.log(num);
//        num++;
//    },1000);


    //用途:setInterval()循环定时器;周而复始的执行(循环执行)
    //用途:setTimeout() 炸弹定时器;用完以后立刻报废(只执行一次)


    //定义方法1(匿名函数)
//    setInterval(function () {
//        console.log(1);
//    },1000);


//    //定义方法2
//    setInterval(fn,1000);
//    function fn(){
//        console.log(2);
//    }

    //定义方法3
//    setInterval("fn(3)",1000);
//    function fn(aaa){
//        console.log(aaa);
//    }

</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <script>
        //返回值,清除定时器。
        var num = 1;

        //setInterval他的返回值就是定时器的名字
        var timer = setInterval(function () {
            console.log(num);
            num++
            if(num===10){
                //如何停止定时器呢???
                clearInterval(timer);
            }
        },500);


    </script>
</body>
</html>
setInterval

相关文章

网友评论

      本文标题:dom_8 定时器

      本文链接:https://www.haomeiwen.com/subject/wyyjqqtx.html