为什么在类变量中引用setInterval为null? [处于保留状态]

问题描述 投票:0回答:1

我在javascript中有一个类,该类正在调用setInterval方法并将对它的引用存储为类变量。但是,当我在另一个方法(stop)中调用变量时,它为null:

export default class {
  constructor() {
    this.intervalRef = null;
  }

  start = (interval, func) => {
    this.intervalRef = setInterval(() => {
      func();
    }, interval);
  };

  stop = () => {
    console.log(this.intervalRef); // => null
    clearInterval(this.intervalRef);
  };
}

为什么?

谢谢

javascript setinterval
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.