写一个mySetInterval(fn, a, b, n)
每次间隔a, a+b, a+nb
执行fn函数 执行n次之后 自动关闭定时器
class MySetInterval {
constructor(fn, a, b, n){
this.fn = fn;
this.a = a;
this.b = b;
this.n = n;
this.count = 0;
this.timer = null;
}
start(){
const time = this.a + this.count * this.b
this.timer = setTimeout(()=>{
this.count++
this.fn()
console.log(time)
this.start()
}, time)
if(this.count > this.n){
clearTimeout(this.timer)
}
}
}
const mySetTimeInterval = new MySetInterval(()=>{
console.log('123123123')
},1000, 2000, 3)
mySetTimeInterval.start()
留言