lgwebdream / FE-Interview

🔥🔥🔥 前端面试,独有前端面试题详解,前端面试刷题必备,1000+前端面试真题,Html、Css、JavaScript、Vue、React、Node、TypeScript、Webpack、算法、网络与安全、浏览器
https://lgwebdream.github.io/FE-Interview/
Other
6.82k stars 896 forks source link

typescript #992

Closed MrDotYan closed 3 years ago

MrDotYan commented 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)