这可能是因为您没有清除setInterval。在路由跳转时,组件可能会被卸载,但setInterval仍在后台运行。为了解决这个问题,您可以在组件卸载时清除setInterval。例如:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      intervalId: null
    };
  }

  componentDidMount() {
    const intervalId = setInterval(() => {
      console.log("Hello World");
    }, 1000);
    this.setState({ intervalId });
  }

  componentWillUnmount() {
    clearInterval(this.state.intervalId);
  }

  render() {
    return <div>My Component</div>;
  }
}

在这个例子中,我们在组件挂载时设置了一个interval,并将其ID存储在组件状态中。当组件卸载时,我们使用clearInterval来清除interval。这样,在路由跳转时,组件将被卸载并清除interval,避免了间歇性执行的问题。

路由跳转后 setInterval不销毁

原文地址: http://www.cveoy.top/t/topic/f1M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录