【发布时间】:2022-01-05 10:29:46
【问题描述】:
我正在尝试有条件地清除和设置Interval。但是,当我这样做时,似乎 clearInterval 看不到 setInterval。
当我将清除间隔移出 if 语句时,它可以工作,但现在不能再有条件地清除设置的间隔。我在这里错过了什么?
// Called when user clicks on menu item
const loadQueueStatus = async () => {
// This function will fetch queueStatus data
const queueStatus = async () => {
// eslint-disable-next-line
const queueStatus = await getQueueStatus().then((data) => {
this.setState({queueStatus: data})
});
}
console.log("state is: ", this.state.isVolunteerStatusOpen)
// call the queue right away if the menu just opened, we know if it is opened as state would still say false
// We do this so we don't wait 5 seconds for the first fetch.
if(!this.state.isVolunteerStatusOpen) {
queueStatus()
}
//Set a listener, to pole the data while menu is open
let setListener = setInterval( queueStatus, 5000);
// If the menu is closed, the state will be true, if so, clear the listener
if(this.state.isVolunteerStatusOpen) {
console.log("clearing listener")
clearInterval(setListener);
}
// Extra caution to clear listener
setListener = null
// Inform state of status window status for next click
this.setState({
isVolunteerStatusOpen: !this.state.isVolunteerStatusOpen
})
}
【问题讨论】:
-
该示例未显示问题。请确保代码是minimal reproducible example?
-
抱歉,我不明白。这就是问题所在。如果我要运行它,setInteral 不会停止,它会继续运行函数 queueStatus。
-
我们无法复制您从提供的代码中描述的问题。因此,提供帮助变得更加困难
-
好的,我添加了整个块。
-
抱歉,仔细阅读代码后,我发现了问题。当我清除间隔时,我已经创建了一个新的侦听器。回到绘图板
标签: javascript reactjs setinterval