我在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);
};
}
为什么?
谢谢