Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.43k stars 3.29k forks source link

能开启多个定时器,可清除 #543

Open toRefs opened 3 years ago

toRefs commented 3 years ago

function _setInterval (handle, delay) { let timer = { id: null } handle() function run () { timer.id = setTimeout(() => { run() handle() }, delay) }

run() return timer }

function _clearSetInterval ({ id }) { clearTimeout(id) }

let t = _setInterval(() => { console.log('hah') }, 1000)

setTimeout(() => { _clearSetInterval(t) }, 4000)

let t2 = _setInterval(() => { console.log('22222') }, 1000)

setTimeout(() => { _clearSetInterval(t2) }, 6000)