gpt4 book ai didi

javascript - 仅在一个组件中调用方法

转载 作者:行者123 更新时间:2023-12-01 01:56:58 25 4
gpt4 key购买 nike

我的网络应用程序的主页组件上有一个计时器,它正在执行一些倒计时。这是 Home 组件:

export default class Home extends React.Component {

timer() {
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
var x = setInterval(() => {

var now = new Date().getTime();

var distance = countDownDate - now;

var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

document.getElementById("timer").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
}, 1000);
}

componentDidMount() {
$('.monster').fadeIn('slow');
this.timer();
}


render() {
return (
<div id="timer"></div>
)
}
}

这是我的问题组件:

export default class AssignmentsComponent extends React.Component {
render() {
return (
<h1>Questions Component</h1>
)
}
}

这是我的 App.js 组件:

export default class App extends Component {
render() {
return (
<div className="App">
<NavigationComponent {...this}/>
{/*<CheckIfHasCurrentTasksComponent user={firebase.auth().currentUser}/>*/}
{/*{this.state.user ? <Questions/> : <Home/>}*/}
</div>
);
}
}

以防万一,这是我的 Navigation 组件:

export default class NavigationComponent extends React.Component {

render() {
return (
<BrowserRouter>
<React.Fragment>
<Navbar className="fixedTop custom_navbar half_transparent">
<Nav className="float_right">
<NavItem className="custom_button_navbar"><span>pobierz</span></NavItem>

{
firebase.auth().currentUser !== null ?
<React.Fragment>
<LinkContainer id="assignments" to='/assignments'>
<NavItem>Zadania</NavItem>
</LinkContainer>
<NavItem onClick={logout.bind(this)}>Wyloguj się</NavItem>
</React.Fragment>
:
<NavItem onClick={this.openLogin}
className="custom_button_navbar"><span>Zaloguj się</span></NavItem>
}

</Nav>
</Navbar>
<Switch>
<Route exact path="/assignments" render={
() => (firebase.auth().currentUser === null ?
<Redirect to='/'/> : <AssignmentsComponent/>)
}/>

<Route exact path='/assignment/:id' render={
props => (
firebase.auth().currentUser === null ?
<Redirect to='/'/> : <CheckIfHasCurrentTasksComponent {...props}/>
)
}/>

<Route exact path="/" component={HomeComponent}/>
</Switch>
<LoginFormComponent show={this.state.show} changeShowState={this.changeShowState}/>
</React.Fragment>
</BrowserRouter>
)
}
}

问题是什么:当我打开主页组件时,一切顺利,计时器启动,一切都很漂亮。但是,如果我转到问题组件,timer() 方法仍在尝试运行,即使我没有在 Assignments 组件中的任何位置调用它。这就是应用程序崩溃的时候。

在timer()方法中添加一个try catch block ,如下所示:

try {
document.getElementById("timer").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
} catch (e) {

}

解决了问题,但我很确定这是一个不好的做法。

那么如何才能使其仅在渲染 Home 组件时调用呢?

最佳答案

您应该在组件卸载时删除计时器。您可以在 Home 组件的 componentWillUnmount() 生命周期方法中执行此操作。

参见:

https://reactjs.org/docs/react-component.html#componentwillunmount

Stop setInterval call in JavaScript

关于javascript - 仅在一个组件中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50943602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com