const TestComponent: React.FC = (props) => {
const [count, setCount] = useState(100);
useEffect(() => {
const interval = setInterval(() => {
if (count === 98) {
prompt("helloworld?");
clearInterval(interval);
}
setCount((count: number) => count - 1);
}, 1000);
return () => {
clearInterval(interval);
};
}, [count]);
return <div>{count}</div>;
};
This is my sample code.
I was hoping the interval function keeps running no matter prompter is shown or not.
But as question says, the component gets paused and timer gets stopped until the prompter is resolved by user input.
Is there a way to keep interval runs as well as the component keeps render a new state?
And more deeper, I am also curious about what causes this behavior.
I guessed it comes from something related single threaded aspect of javascript but as I said, it is just guessing.