题解 | #计时器#

计时器

https://www.nowcoder.com/practice/72c661d926494bd8a50608506915268c

知识点 clearInterval() setInterval()

// 返回的是对象 {cancel}

// if (start < end)在定时器外面的话 会多输出一个数字

<script>
    function count(start, end) {
        let i = 0
        console.log(start)
        function cancel() {
            clearInterval(i)
        }
        i = setInterval(function () {
            if (start < end) {  // if 在定时器外面的话 会多输出一个数字
                ++start
                console.log(start)
            }
        }, 100)
        return { cancel }
    }
</script>

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务