I have following code in my react component:
//some code
var timeout = null; //global variable
//some more code
useEffect(() => {
if(...some condition) {
console.log('test1');
timeout = setTimeout(() => {
console.log('test2');
}, 15 * 1000);
} else {
console.log('test3');
clearTimeout(timeout);
}
}, [some other condition]);
The output I get in the console:
test1
test3
test2 //after 15 seconds
So I am trying to start timeout which will log 'test2', if in the meantime timeout was not cleared. As we can see 'test3' is logged, meaning timeout was cleared, but still 'test2' is logged after 15 seconds. What is the issue here?