Closed MrDotYan closed 3 years ago
class SetInterValForMe { private timer!: any; private time!: number; public SetInterValForMe(fn: Function, a: number, b: number) { this.time = 0; this.start(fn, a, b) } public start(fn: Function, a: number, b: number) { this.timer = setTimeout(() => { this.time++; fn && fn(); clearTimeout(this.timer); this.start(fn, a, b); }, a + this.time * b) } public stop() { clearTimeout(this.timer); } } // test demo let timer = new SetInterValForMe(); timer.SetInterValForMe(() => { console.log((new Date()).getSeconds()); }, 1000, 200); setTimeout(() => { timer.stop() }, 5000)