本文由兩部分組成,思路和代碼
思路:
1.運用componentWillReceiveProps方法的監聽props改變的功能,
2.這裏倒計時要用setTimeout,不能用setInterval。
代碼:
componentWillReceiveProps(next){
//短信驗證碼倒計時
if(next.isTimerStart && next.timerNum >= 0) {//isTimerStart爲是否開啓倒計時功能,timerNum爲倒計時顯示的變化秒數,默認爲60
this.timer= setTimeout(() => {
let val = next.timerNum - 1;
if(val < 0){
clearTimeout(this.timer);//清楚定時器
}else{
this.props.setTimeNum(val);//通過redux改變倒計時的秒數
}
},1000);
}
}