Javascript 在ComponentDidMount中停止并启动设置间隔
我目前正在制作一个React应用程序,它每秒从API获取数据以更新自身。 然后,我将显示一个表,其中包含每秒刷新的数据 我目前的问题是,我必须允许用户点击td,从而停止计时器,允许他编辑。 完成后,再次启动计时器 我可以停止它,但我不知道如何重新启动它一旦停止。这是我的密码:Javascript 在ComponentDidMount中停止并启动设置间隔,javascript,reactjs,timer,Javascript,Reactjs,Timer,我目前正在制作一个React应用程序,它每秒从API获取数据以更新自身。 然后,我将显示一个表,其中包含每秒刷新的数据 我目前的问题是,我必须允许用户点击td,从而停止计时器,允许他编辑。 完成后,再次启动计时器 我可以停止它,但我不知道如何重新启动它一旦停止。这是我的密码: class App extends Component { state = { data: [], } componentDidMount() { this.timer = setInterv
class App extends Component {
state = {
data: [],
}
componentDidMount() {
this.timer = setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
.then(data => this.setState({ data: data.data }))
.catch(error => console.log(error))), 1000)
}
stopTimer = () => {
console.log("stoooop")
clearInterval(this.timer)
}
launchTimer = () => {
???
}
哦,当然,我陷入了在componentDidMount中完成这一切的奇怪逻辑中,甚至没有想到这一点。。谢谢!哦,当然,我陷入了在componentDidMount中完成这一切的奇怪逻辑中,甚至没有想到这一点。。谢谢!
class App extends Component {
state = {
data: [],
}
componentDidMount() {
this.timer = this.launchTimer();
}
stopTimer = () => {
console.log("stoooop");
clearInterval(this.timer);
};
launchTimer = () => {
this.timer = setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
.then(data => this.setState({ data: data.data }))
.catch(error => console.log(error))), 1000);
};
class App extends Component {
state = {
data: [],
}
componentDidMount() {
this.timer = launchTimer();
}
stopTimer = () => {
console.log("stoooop")
clearInterval(this.timer)
}
launchTimer = () => {
setInterval(() => (axios.get("http://127.0.0.1:8000?count=20")
.then(data => this.setState({ data: data.data }))
.catch(error => console.log(error))), 1000)
}