joschan21 / similarity-api

https://www.similarityapi.com/
630 stars 128 forks source link

return () => clearInterval(intervalId) #13

Open andy27182 opened 5 months ago

andy27182 commented 5 months ago

Hi, I'm studying your videos, thanks a lot, but when I started the Code.tsx part, there is a little doubt that return () => clearInterval(intervalId) is used as the return value of the callback function for setTimeout. I think it will not work and should it be used as the return value of the useEffect callback function for clearing the memory.

useEffect(() => {
    if (show && animated) {
      let i = 0
      setTimeout(() => {
        const intervalId = setInterval(() => {
          setText(code.slice(0, i))
          i++
          if (i > code.length) {
            clearInterval(intervalId)
          }
        }, 15)

        return () => clearInterval(intervalId)
      }, animationDelay || 150)
    }
  }, [code, show, animated, animationDelay])