Merge pull request #2607 from MartinFournier/fix/corruptable-unmount

Clear timers when unmounting CorruptableText
This commit is contained in:
hydroflame 2022-01-13 11:25:35 -05:00 committed by GitHub
commit 4555354957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -25,20 +25,22 @@ export function CorruptableText(props: IProps): JSX.Element {
useEffect(() => { useEffect(() => {
let counter = 5; let counter = 5;
const id = setInterval(() => { const timers: number[] = [];
const intervalId = setInterval(() => {
counter--; counter--;
if (counter > 0) return; if (counter > 0) return;
counter = Math.random() * 5; counter = Math.random() * 5;
const index = Math.random() * content.length; const index = Math.random() * content.length;
const letter = content.charAt(index); const letter = content.charAt(index);
setContent((content) => replace(content, index, randomize(letter))); setContent((content) => replace(content, index, randomize(letter)));
setTimeout(() => { timers.push(window.setTimeout(() => {
setContent((content) => replace(content, index, letter)); setContent((content) => replace(content, index, letter));
}, 500); }, 500));
}, 20); }, 20);
return () => { return () => {
clearInterval(id); clearInterval(intervalId);
timers.forEach((timerId) => clearTimeout(timerId));
}; };
}, []); }, []);