#Why does this countdown start glitching out?

3 messages · Page 1 of 1 (latest)

karmic hornet
#

Im trying to make a countdown and it starts glitching out and using a lot of CPU.

#

when I go for this it just gets stuck on 119:

const Clock = () => {
  const [time, setTime] = useState(120)
  const interval = useRef();

  useEffect(() => {

    interval.current = setInterval(() => {
      setTime(time - 1)
    }, 1000)

    return () => clearInterval(interval.current)
  }, [])

  if (time === 0) clearInterval(interval.current)
  return (
    <div>
      {time}
    </div>
  )
};
#

oh adding time to dependencies seems to fix it