【资料图】
本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。
react关闭页面时间怎么设置?
react中实现简单倒计时关闭页面
首先在constructor中设置5秒的时间值
constructor (props) { super(props) this.state={ seconds: 5, dlgTipTxt: "5s后关闭页面" }; }
登录后复制
componentDidMount中添加定时器
componentDidMount () { let timer = setInterval(() => { this.setState((preState) =>({ seconds: preState.seconds - 1, dlgTipTxt: `${preState.seconds - 1}s后自动关闭`, }),() => { if(this.state.seconds == 0){ clearInterval(timer); window.close() } }); }, 1000)}
登录后复制
render中添加判断
render() {return ({this.state.dlgTipTxt}}/>)}
登录后复制
推荐学习:《react视频教程》
以上就是react关闭页面时间怎么设置的详细内容,更多请关注php中文网其它相关文章!