(编辑:jimmy 日期: 2024/12/25 浏览:2)
需求分析
需求
在某个页面中需要有一个倒计时的功能,倒计时 5 s,5s钟后跳转到新的界面
分析
初版做法
代码
let waitTime = 5 class DemoPage extends React.Component { constructor(props) { super(props); this.state = { time: '', }; } componentDidMount = () => { this.countDown(); }; countDown = () => { if (waitTime > 0) { waitTime--; this.setState({ time:waitTime }) } else { history.push('/Login') return; } setTimeout(() => { this.countDown(); }, 1000); } render() { todoInfo = this.state.time + '秒后跳转至登录界面'; return ( <div> todoInfo </div> ); } } export default DemoPage;
问题分析
时间设置为全局变量,糟糕的做法,
改进版
代码
class DemoPage extends React.Component { constructor(props) { super(props); this.state = { time: '', }; } componentDidMount = () => { this.countDown(5);//倒计时时间可随意调整,且可读性强 }; countDown = (waitTime) => { if (waitTime > 0) { waitTime--; this.setState({ time:waitTime }) } else { history.push('/Login') return; } setTimeout(() => { this.countDown(waitTime); }, 1000); } render() { todoInfo = this.state.time + '秒后跳转至登录界面'; return ( <div> todoInfo </div> ); } } export default DemoPage;
改进后将时间作为参数放到countDown里面,方便随意设置倒计时时间
进一步分析问题:
上面的做法,
进一步改进
针对本问题的需求,可以将业务场景扩大为:
doSomethingDuringCountDown()
doSomethingAfterCountDown()
这样的话,倒计时的功能就可以使用的更加的灵活了。
方案
将函数作为参数传递到countDown()方法中
将 doSomethingDuringCountDown()
和 doSomethingAfterCountDown()
作为参数传递到countDown方法中,
具体的方法实现,根据自己页面的需求来实现。
代码
//utils.js export countDown = (waitTime,doSomethingDuringCountDown,doSomethingAfterCountDown){ if (waitTime > 0) { waitTime--; if(doSomethingDuringCountDown){ doSomethingDuringCountDown() } } else { if(doSomethingAfterCountDown){ doSomethingAfterCountDown() } return; } setTimeout(() => { countDown(waitTime,doSomethingDuringCountDown,doSomethingAfterCountDown); }, 1000); }
实例
//DemoPage.jsx import { countDown } from 'utils.js' class DemoPage extends React.Component { constructor(props) { super(props); this.state = { time: '', }; } componentDidMount = () => { countDown(5,this.waitTimeStateChange,this.linkTo); } waitTimeStateChange = (time) => { this.setState({ time: time, }) } linkTo = () => { history.push(ToBeReviewedShowData.linkUrl) } render() { todoInfo = this.state.time + '秒后跳转至登录界面' return ( <div> todoInfo </div> ) } } export default DemoPage
总结