javascript - reactjs中设置时间间隔的问题
问题描述
我试图每隔 5 秒在设定的时间间隔内调用一个函数,但我在 TypeError 中抛出错误:this.intialState is not a function
componentDidMount() {
this.intialState();
setInterval(this.changeSelection,5000);
}
changeSelection(){
this.intialState();
}
TypeError: this.intialState is not a function
解决方案
使用更新的 5 秒倒计时class Clock extends Component
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;
推荐阅读
- reactjs - 循环内的 expo SMS.sendSMSAsync 弹出短信模式并立即关闭
- git - 如何将推送恢复为 master,然后再次从新分支推送相同的更改?
- python - pyodbc ValueError: 月份必须在 1.. 12
- python - 如何使用 SQLite3 和 Python 更新 SQLite 表中的行
- c - Intel-TSX:为什么 rtm 失败了?
- ssl - ESP32-CAM 如何将大型二进制有效负载发布到 AWS IOT ssl mqtt 主题,测试了许多库但没有成功:-(
- html - 如果表格中的单元格中有文本,如何将文本添加到与单元格最右侧对齐的同一单元格中?
- javascript - 当我将它传递给套接字 io 时,对象失去了它的原型功能
- octave - 绘制 f(x,y,z) 消失的 3D 曲面
- javascript - 在 Twilio 中获取两个数字之间的消息