react中简单倒计时跳转
发布时间:2020-12-15 20:23:45 所属栏目:百科 来源:网络整理
导读:其实在react中实现倒计时的跳转方法有很多中,其中我认为较为好用的就是通过定时器更改state中的时间值。 首先在constructor中设置10秒的时间值: constructor () { super() this.state={ seconds: 10,}; } ? 然后在componentDidMount中添加定时器: compone
其实在react中实现倒计时的跳转方法有很多中,其中我认为较为好用的就是通过定时器更改state中的时间值。 首先在constructor中设置10秒的时间值: constructor () { super() this.state={ seconds: 10,}; } ? 然后在componentDidMount中添加定时器: componentDidMount () { let timer = setInterval(() => { this.setState((preState) =>({ seconds: preState.seconds - 1,}),() => { if(this.state.seconds == 0){ clearInterval(timer); } }); },1000) } ? 然后在render中添加判断跳转 if (this.state.seconds === 0) { window.location.href=‘http://www.cnblogs.com/a-cat/‘; } ? 这种就可以完成倒计时跳转了! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |