题解 | #计时器#
计时器
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>
