请帮助我如何在settimeout之后执行该功能

问题描述 投票:0回答:1
有一个带有计时器的弹出窗口和一个指示器,该窗口已在时间经过之后关闭。如何使

cancelOrder

函数运行并关闭窗口?

const cancelOrder = async () => { if (isAcceptedRef.current === false) { await changeAnswerStatus(answer.id, 0); onCancel(); } }; useEffect(() => { if (time > 0) { const timer = setTimeout(() => setTime(time - 0.1), 100); return () => { clearTimeout(timer); }; } else { setHidden(true); } }, [time]); if (loading) { return "Loading..."; } if (hidden) { return null; } return ( <li className={cx( "bg-paper rounded-xl flex flex-col p-2.5 space-y-1.5 shadow-history", answer.isNavigatorMode && "ring ring-red", )} > <div> <span className="rounded-full transition-all h-1 bg-blue block" style={{ width: `calc(100% / 15 * ${time})`, }} /> </div> </li> );
    
reactjs
1个回答
0
投票
simply call cancelorder()在您验证那个时间的同一时刻

<= 0:

useEffect(() => { if (time > 0) { const timer = setTimeout(() => setTime(time - 0.1), 100); return () => { clearTimeout(timer); }; } else { cancelOrder(); setHidden(true); } }, [time]);
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.