I have created one setInterval which is being called on App Load, Like this
let abc = true;
let def = true;
const intervalRef = useRef(null);
if (abc) {
intervalRef.current = setInterval(() => {
console.log("hello console", def);
}, 2000);
abc = false;
}
And trying to stop this interval using clearInterval on some click handler like this -
const clear = () => {
clearTimeout(intervalRef.current);
def = false;
console.log(def);
};
Issues I am facing right now are -
- Why SetInterval is not stopped on click handler, while I am already cleared it in function?
- Why value of
defin not getting updated in setInterval.
PS: I am new in react, if this is silly mistake forgive me :)