我尝试每 5 秒在设定的时间间隔内调用一个函数,但我抛出了错误 类型错误:this.initialState 不是一个函数
componentDidMount() {
this.intialState();
setInterval(this.changeSelection,5000);
}
changeSelection(){
this.intialState();
}
TypeError: this.intialState is not a function
最佳答案
使用class Clock extends Component
更新了5秒倒计时
import React, { Component } from 'react';
class Clock extends Component {
constructor(props){
super(props);
this.state = {currentCount: 10}
}
timer() {
this.setState({
currentCount: this.state.currentCount - 1
})
if(this.state.currentCount < 1) {
clearInterval(this.intervalId);
}
}
componentDidMount() {
this.intervalId = setInterval(this.timer.bind(this), 1000);
}
componentWillUnmount(){
clearInterval(this.intervalId);
}
render() {
return(
<div>{this.state.currentCount}</div>
);
}
}
export default Clock;
关于javascript - 在reactjs中设置时间间隔的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55861182/