React 60s倒計時


 
constructor(props) {
  super(props);
  this.state = {
    tel: 13407163853,
    btnText: '獲取驗證碼',
    timer: 60,
    discodeBtn: false,
    clearInterval: false
  }
}
...
componentWillReceiveProps(nextProps) {
  const { props } = this;
  const { openBox } = nextProps;
  // console.log('nextProps', nextProps);
  // 獲取驗證碼
  if (props.openBox.sendBoxMobileCode !== openBox.sendBoxMobileCode) {
    const { code, message } = openBox.sendBoxMobileCode;
    if (code === '0') {
      this.count();
    } else {
      Toast.info(message);
    }
  }
}
 
count = () => {
let siv = setInterval(() => {
    this.setState({ timer: (timer--), btnText: timer, discodeBtn: true }, () => {
        if (timer === 0) {
            clearInterval(siv);
            this.setState({ btnText: '重新發送', discodeBtn: false })
        }
    });
}, 1000);
}
 
...
render() {
  
return (
  <div>
    <input type="button" value={this.state.btnText} disabled={this.state.discodeBtn} onClick={this.sendCode} />
  </div>
)}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM