美文网首页
36. 定时器

36. 定时器

作者: 努力生活的西鱼 | 来源:发表于2019-02-14 20:01 被阅读0次

127. 定时器

<script>
    /**
     * 如果希望一段程序,可以每间隔一段时间执行一次,可以使用定时调用
     */
    window.onload = function () {
        let num = 0;
        let count = document.getElementById("count");
        /**
         * setInterval()
         *  - 定时调用
         *  - 可以将一个函数,每隔一段时间执行一次
         *  - 参数:
         *      1. 回调函数,该函数会每隔一段时间被调用一次
         *      2. 每次调用间隔的时间,单位是毫秒
         *
         *  - 返回值
         *      - 返回一个Number类型的数据
         * @type {number}
         */
        let timer = window.setInterval(function () {
            count.innerHTML = num++;
            if (num == 10) {
                window.clearInterval(timer);
            }
        }, 1000);

        /**
         * clearInterval()
         *  - 用来关闭一个定时器
         *  - 方法中需要一个定时器的标识作为参数,这样将关闭标识对应的定时器
         */

        console.log(timer);
    };

</script>

相关文章

网友评论

      本文标题:36. 定时器

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