使用clearInterval()内部使用效果不起作用。 setInterval()中的功能不断被调用
预期的结果: 我正在从API中获得脚本的状态,然后检查状态是什么。 如果status ='xyz',我需要再次调用getStatus函数(使用setInterval), 否则我只需要返回/什么也不做。
问题: 主要问题是ClearInterval()不起作用,即使状态不等于“ XYZ”,也会一次又一次地调用GetStatus()函数(此处10秒)。
请帮忙。提前致谢。
const [intervalId, setIntervalId] = useState();
useEffect(() => {
getStatus();
}, []);
const getStatus = async () => {
await getScriptStatus()()
.then(response => {
if (response.data[0].status == 'XYZ') {
setIntervalId(
setInterval(() => {
getStatus();
}, 10000)
);
}
else
return () => clearInterval(intervalId);
})
.catch(error => {
errorMessage(error);
});
更新: 在使用“如果”条件下使用setInterval()之前,请使用react.useref()代替usestate()进行间隔和清除间隔,或者我使用。 感谢您的有用答案。
Desired Outcome:
I am getting a script's status from an API and then checking what the status is.
If the status = 'XYZ', I need to call the getStatus function again (using setInterval),
else I just need to return/do nothing.
Issue:
The major issue is that clearInterval() is not working and the getStatus() function is getting called after the mentioned interval (10 seconds here) again and again, EVEN THOUGH the status IS NOT EQUAL to 'XYZ'.
Please help out. Thanks in advance.
const [intervalId, setIntervalId] = useState();
useEffect(() => {
getStatus();
}, []);
const getStatus = async () => {
await getScriptStatus()()
.then(response => {
if (response.data[0].status == 'XYZ') {
setIntervalId(
setInterval(() => {
getStatus();
}, 10000)
);
}
else
return () => clearInterval(intervalId);
})
.catch(error => {
errorMessage(error);
});
UPDATE:
Using React.useRef() instead of useState() for intervalId and clearing interval before using setInterval() in the 'if' condition worked or me.
Thank you for the helpful answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
settimeout
而不是使用setInterval
。有关更多详细信息,请访问:
Use
setTimeout
instead of usingsetInterval
.For more details visit : Documentation on setTimeout
Intervalid
不需要是“状态”,而是存储是参考,而不是状态。then()。catch()
更改为使用async/等待
。setIntervalid
没有更新Intervalid
,当您尝试已经尝试访问时代码> Intervalid 的新值另一个建议:
getStatus
函数不应充当使用效应功能,因为看到getStatus
返回A返回A很困惑函数..然后您需要回到“啊这是一个使用效应功能..返回的功能是一个清理功能..” - 不好..。所以..我建议的选项:
getState
函数更改为GetStatus ..您希望返回一个函数(清理功能)useffect
中。而且由于您想使用async/等待
- >创建一个self-alling-assync函数:useffect(()=> {(async()=> {等待func();});});},[],[];
update>更新:
除了所有这些 ^
您确实不需要
setInterval
(就像其他人建议的那样)。settimeout
会很好。但是您可能仍然需要cleartimeout
(从clear Interval更改),如果可以在不同的情况下调用函数intervalId
doesn't need to be a "state", rather store is as a ref and not a state..then().catch()
to usingasync/await
.setIntervalId
didn't update theintervalId
yet, when you're trying to already accessintervalId
's new valueAnother suggestion:
The
getStatus
function should not act as a useEffect-function, because it's very confusing to see thatgetStatus
returns a function.. and then you need to trail back to "AH it's a useEffect-function.. and the function returned is a cleanup function.. " - not good.. .so.. My Suggested Options for that:
getState
function to only getStatus.. and then when you call it from inside theuseEffect
- also handle the case in which you want to return a function (cleanup function)useEffect
. And since you want to useasync/await
-> create a self-calling-async-function:useEffect(()=>{ (async () => { await func(); })(); }, []);
Update:
In addition to all that ^
You really don't need the
setInterval
(like others have suggested).setTimeout
will do just fine. But you might still want theclearTimeout
(changed from clearInterval) if the functio might be called on different occasions这里的主要问题很可能是您递归安装新的间隔:函数安装呼叫安装间隔等的函数的间隔,有效地产生间隔的级联。它们或多或少是可撤离的,因为它们的ID在安装它们然后被扔掉的功能范围中占据了临时范围。
您可以在。
Main problem here is most probably that you are installing new intervals recursively: function installs interval that calls function that installs interval etc, effectively producing cascade of intervals. These are more or less in-revocable, because their IDs live ephemerally in function scope that installed them and are then thrown away.
You can observe it with slightly modified version of your code in this sandbox.
更改
return()=> clear Interval(Intervalid);
to此clear Interval(Intervalid);
。Change
return () => clearInterval(intervalId);
to thisclearInterval(intervalId);
.