代码如下:
function mySetInterval(fn, a, b){
let timer = {id: null}
function run(fn, a, b){
timer.id = setTimeout(()=>{
fn()
console.log('间隔了' + a + 'ms')
run(fn, a + b, b)
},a)
}
run(fn , a, b)
return timer
}
function myClear(timeId){
clearTimeout(timeId)
}
// test
let timer = mySetInterval(() => {console.log('执行了fn函数')}, 200, 300)
setTimeout(()=>{
myClear(timer.id)
}, 2000)
运行结果截图如下:
参考自:https://juejin.cn/post/6847902225423925255