这很简单。
我正在使用 Redux 来管理我的状态
我有一个setTimeout
函数在一个useEffect
功能。
The setTimeout
超时值为50000
ms.
我想要 SetTimeout 处理程序做什么
After 50000ms
the setTimeout
函数检查是否已收到 api 调用响应。
如果尚未收到回复,则setTimeout
函数应该重新启动 api 调用,因为这样调用就会被视为超时。
回调处理程序正在做什么
After 50000ms
,即使已收到响应,setTimeout 处理程序仍会发起 api 调用。
我尝试记录状态的输出,然后它返回一个缓存的状态,即使该状态已传递到useEffect
功能并且应该已经更新
进行 api 调用后,testDetails.isUpdatingTestDetails
状态设置为false
我尝试了几种逻辑,但没有一个有效
Logic 1
useEffect(() => {
//Notice how i check if the testDetails is being updated before initiating the setTimeout callback
if (testDetails.isUpdatingTestDetails === true) {
setTimeout(() => {
// Inside the settimeout function the same check is also done.
// even though before 50 seconds the response is being received , the function logs the text simulating the reinitiation of an api call
return testDetails.isUpdatingTestDetails === true &&
console.log("After 50 Seconds You Need To Refetch This Data")
}, 50000);
}
}, [testDetails.isUpdatingTestDetails, testDetails])
Logic 2
useEffect(() => {
setTimeout(() => {
return testDetails.isUpdatingTestDetails === true &&
console.log("After 50 Seconds You Need To Refetch This Data")
}, 50000);
}, [testDetails.isUpdatingTestDetails, testDetails])
我上面应用的逻辑都不起作用。