简体   繁体   English

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

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

Alright, So I have a Splash screen which uses setInterval but the problem is setInterval now basically affects my whole navigation in my app I tried to use clearInterval but It won't work. 好了,所以我有一个使用setInterval的启动屏幕,但问题是setInterval现在基本上影响了我尝试使用clearInterval应用程序中的整个导航,但是它不起作用。

I tried changing from componentWillMount to componentDidMount but it still doesn't work. 我试着从改变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);
}

you don't need to clear interval you can just simply 您不需要清除间隔,您只需

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

when you are navigate to another class you can use this and if you want to reset your stack like you don't want splash screen in stack 当您导航到另一个类时,可以使用它,并且如果您想像不希望启动屏幕那样在堆栈中重设堆栈

  componentDidMount(){
    setTimeout(() => {

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

Why are you getting the clearInterval from the props????? 为什么从道具中获得clearInterval?

to clear the setInterval 清除setInterval

do this 做这个

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

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

The clearInterval() function clears the interval which has been set by setInterval() function before that. clearInterval()函数清除之前由setInterval()函数设置的间隔。

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

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