TypeScript 下 vue 定时器 的简单实现

发布于 2020-04-14  883 次阅读


主要是 setInterval 这一函数在 ts 下的简单实现

export default class Test extends tsx.Component { 
  public timer: NodeJS.Timeout | null = null;

  public mounted() {
    this.timer = setInterval(
      () =>
        console.log("doSomething"),
        10000,
      )
    }

  public beforeDestroy() {
    if(this.timer) clearInterval(this.timer)
    // clearInterval(Number(this.timer))
  }
}

Fly me to the moon