繁体   English   中英

clearInterval()不会清除setInterval和影响导航

[英]clearInterval() won't clear setInterval and Affect navigation

好了,所以我有一个使用setInterval的启动屏幕,但问题是setInterval现在基本上影响了我尝试使用clearInterval应用程序中的整个导航,但是它不起作用。

我试着从改变componentWillMountcomponentDidMount ,但它仍然无法正常工作。

componentDidMount(){
  const splashInterval = setInterval(()=>{
      this.props.navigation.navigate('Main');
    },2000);
    this.setState({splashInterval: splashInterval});
}


componenWillUnmount(){
  const splashInterval = this.state.splashInterval;
  const clearSplashInterval = this.props.navigation.clearInterval(splashInterval);
  this.setState(clearSplashInterval);
}

您不需要清除间隔,您只需

  componentDidMount(){
    setTimeout(() => {
this.props.navigation.navigate("Main")
    }, 100);
  }

当您导航到另一个类时,可以使用它,并且如果您想像不希望启动屏幕那样在堆栈中重设堆栈

  componentDidMount(){
    setTimeout(() => {

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: 'main' })],
});
this.props.navigation.dispatch(resetAction);
    }, 100);
  }

为什么从道具中获得clearInterval?

清除setInterval

做这个

componentDidMount(){
 this.inter = setInterval(async() => {
            this.props.navigation.navigate('Main');
        }, 2000);
}

componenWillUnmount(){
  clearInterval(this.inter);
}

clearInterval()函数清除之前由setInterval()函数设置的间隔。

暂无
暂无

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

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