diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx index 904af9b..430f946 100644 --- a/components/Editor/index.tsx +++ b/components/Editor/index.tsx @@ -77,6 +77,7 @@ const Editor = ({ readOnly = false }: Props) => { const editorRef = useRef() const [showArgumentsHelper, setShowArgumentsHelper] = useState(false) + const timeoutRef = useRef(null) useEffect(() => { const query = router.query @@ -293,7 +294,10 @@ const Editor = ({ readOnly = false }: Props) => { } setCairoCode(lines.join('\n')) - const timeoutId = setTimeout( + if (timeoutRef.current) { + clearTimeout(timeoutRef.current) + } + timeoutRef.current = setTimeout( () => textareaRef.setSelectionRange( selectionStart + charOffsetStart, @@ -301,8 +305,6 @@ const Editor = ({ readOnly = false }: Props) => { ), 0, ) - - return () => clearTimeout(timeoutId) }, [cairoCode]) useEffect(() => { @@ -318,6 +320,14 @@ const Editor = ({ readOnly = false }: Props) => { } }, [handleCommentLine, cairoCode]) + useEffect(() => { + return () => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current) + } + } + }, []) + return ( <>