Reactjs 类型';上不存在此.setinterval;定时器';类内组件

Reactjs 类型';上不存在此.setinterval;定时器';类内组件,reactjs,typescript,Reactjs,Typescript,我正在尝试使用reactjs和typescript制作一个计时器。我的程序中有定时器类组件。我的密码在这里。我想使用myInterval函数,在该函数中我应该使用setInterval()函数来设置计时器。我面临的问题就在这个.myInterval中。它显示类型计时器上不存在myInterval。请帮帮我 interface IProps {} interface IState { minutes: number; seconds: number; isOn: boolean; }

我正在尝试使用reactjs和typescript制作一个计时器。我的程序中有定时器类组件。我的密码在这里。我想使用
myInterval
函数,在该函数中我应该使用
setInterval()
函数来设置计时器。我面临的问题就在这个.myInterval中。它显示类型计时器上不存在
myInterval
。请帮帮我

interface IProps {}

interface IState {
  minutes: number;
  seconds: number;
  isOn: boolean;
}

class Timer extends React.Component<IProps, IState> {
  constructor(props: any) {
    super(props);
    this.state = {
      minutes: 25,
      seconds: 0,
      isOn: false,
    };

    this.startTimer = this.startTimer.bind(this);
    this.stopTimer = this.stopTimer.bind(this);
    this.resetTimer = this.resetTimer.bind(this);
  }

  startTimer() {
    if (this.state.isOn === true) {
      return;
    }

    this.myInterval =  () => setInterval(() => {
      const { seconds, minutes } = this.state;

      if (seconds > 0) {
        this.setState(({ seconds }) => ({
          seconds: seconds - 1,
        }));
      }
      if (seconds === 0) {
        if (minutes === 0) {
          clearInterval(this.myInterval);
        } else {
          this.setState(({ minutes }) => ({
            minutes: minutes - 1,
            seconds: 59,
          }));
        }
      }
    }, 1000);
    this.setState({ isOn: true });
  }

  stopTimer() {
    clearInterval(this.myInterval);
    this.setState({ isOn: false });
  }

  resetTimer() {
    this.stopTimer();
    this.setState({
      minutes: 25,
      seconds: 0,
    });
  }
接口IProps{}
界面状态{
分钟:数字;
秒:数字;
伊森:布尔;
}
类计时器扩展了React.Component{
构造器(道具:任何){
超级(道具);
此.state={
会议记录:25,
秒:0,
伊森:错,
};
this.startTimer=this.startTimer.bind(this);
this.stopTimer=this.stopTimer.bind(this);
this.resetTimer=this.resetTimer.bind(this);
}
startTimer(){
if(this.state.isOn===true){
返回;
}
this.myInterval=()=>setInterval(()=>{
const{seconds,minutes}=this.state;
如果(秒>0){
this.setState(({seconds})=>({
秒:秒-1,
}));
}
如果(秒===0){
如果(分钟===0){
clearInterval(this.myInterval);
}否则{
this.setState(({minutes})=>({
分钟:分钟-1,
秒:59,
}));
}
}
}, 1000);
this.setState({isOn:true});
}
停止计时器(){
clearInterval(this.myInterval);
this.setState({isOn:false});
}
重置计时器(){
这个.stopTimer();
这是我的国家({
会议记录:25,
秒:0,
});
}
有两个问题

  • 您从未为myInterval设置字段,因此this.myInterval不存在
  • 将a函数调用设置为myInterval,而不是从setInterval返回
  • 这就是代码的外观

      //myInterval field
      myInterval:NodeJS.Timer;
    
      startTimer() {
        if (this.state.isOn === true) {
          return;
        }
    
        //Assign the return value from setInterval to myInterval
        this.myInterval = setInterval(() => {
         const { seconds, minutes } = this.state;
    
          if (seconds > 0) {
            this.setState(({ seconds }) => ({
              seconds: seconds - 1,
            }));
          }
          if (seconds === 0) {
            if (minutes === 0) {
             clearInterval(this.myInterval);
           } else {
             this.setState(({ minutes }) => ({
               minutes: minutes - 1,
               seconds: 59,
             }));
           }
         }
       }, 1000);
       this.setState({ isOn: true });
    }