我想做一個(gè)類似倒計(jì)時(shí)的效果,當(dāng)?shù)褂?jì)時(shí)為0的時(shí)候,計(jì)時(shí)器停止,但是目前不知道怎么做,求大神解答,謝謝!新手求助!目前有如下代碼:const totalCount = 10;class Register extends Component { ... // 構(gòu)造 constructor(props) { super(props); // 初始狀態(tài) this.state = { count: totalCount }; this.componentWillUnMount = this.componentWillUnMount.bind(this); } componentWillUnMount() { clearInterval(this.timer);//FIXME:無法實(shí)現(xiàn)卸載時(shí)清除計(jì)時(shí)器 } count() { this.timer = setInterval(()=>this.setState({ count: this.state.count - 1 } ), 1000); if (this.state.count == 0) { //clearTimeout(this.timer)//FIXME:確定程序可以走到這里,但是無法實(shí)現(xiàn)清除計(jì)時(shí)器 clearInterval(this.timer); } } render() { return( ... <TouchableOpacity onPress={this.count.bind(this)} disabled={!(this.state.count == totalCount || this.state.count == 0)}> ... </TouchableOpacity> ) }}
在react native中,使用es6語法怎么清除定時(shí)器?
慕妹3146593
2018-09-13 09:25:21