简体   繁体   English

反应本机SetTimeOut

[英]React Native SetTimeOut

I want to use timer within my RN apps. 我想在我的RN应用中使用计时器。 I want to show waiting... message top of the list view. 我想显示waiting...消息在列表视图的顶部。 Here is the scenario, wait 2.5 second for adding new item to list, at the same time show info message to user, then wait 1 seconds for next cycle and don't show waiting... message. 在这种情况下,等待2.5秒钟将新项添加到列表中,同时向用户显示信息消息,然后等待1秒钟进行下一个循环,并且不显示waiting...消息。 How can I do this? 我怎样才能做到这一点? I tried many combination but it never works as expected. 我尝试了许多组合,但从未按预期进行。

render(){
        return (
          <View style={styles.container}>
            <Text style={styles.waiting}>
              {'waiting...'}
            </Text>
            <ListView
              dataSource={this.state.dataSource}
              renderRow={this.renderCell}
              automaticallyAdjustContentInsets={false}
              style={styles.listView}
              />
          </View>
        );
      }

If I understand correctly, give it a try this code. 如果我理解正确,请尝试以下代码。 Hope this help... 希望这个帮助...

getInitialState: function() {
   return {
     showLoading : true,//To show 'Waiting Text in initial state.
   };
},

componentDidMount() {
   this.setTimeout( () => {
      this._hideLoadingView();//To hide 'Waiting' Text after 1 second
   },1000);
},

_hideLoadingView() {
   this.setState({
     showLoading: false, //set state to hide 'Waiting'
   });
},


render(){
    return (
      <View style={styles.container}>
        {this._loadingTextView()}
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this.renderCell}
          automaticallyAdjustContentInsets={false}
          style={styles.listView}
          />
      </View>
    );
  },

_loadingTextView(){
  if(this.state.showLoading){//To show or hide 'Waiting'
  return( 
     <Text style={styles.waiting}>
     Waiting.....
     </Text>
   )
  }
}

Phyo is almost right. Phyo几乎是正确的。 The only problem being that it will happpen only once. 唯一的问题是它只会发生一次。 I will not cycle between the two. 我不会在两者之间循环。 The following might get the trick done 以下可能会解决问题

getInitialState: function() {
   return {
     showLoading : true,//To show 'Waiting Text in initial state.
   };
},

 componentWillMount() {
  this._showLoadingView; // start by displaying the wait
},

_hideLoadingView() {
   this.setTimeout(this._showLoadingView,1000);
   this.setState({
     showLoading: false, //set state to hide 'Waiting'
   });
},
_showLoadingView(){
   // Do other fetch instructions here
   this.setTimeout(this._hideLoadingView,2500);
   this.setState({
     showLoading: true,
   });
},
render(){
    return (
      <View style={styles.container}>
        {this.textRender()}
        <ListView
         dataSource={this.state.dataSource}
         renderRow={this.renderCell}
         automaticallyAdjustContentInsets={false}
         style={styles.listView}
         />
      </View>
    );
  },

textRender(){
  if(this.state.showLoading){//To show or hide 'Waiting'
      return( 
         <Text style={styles.waiting}>
             Waiting.....
         </Text>
       )
  }
  else{
      return <View />
  }

}

Remember to use the TimerMixin if you want to use this.setTimeout (recommended) else use setTimeout 如果要使用this,请记住使用TimerMixin.setTimeout(推荐),否则请使用setTimeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM