Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

My React countdown timer sticks, skips seconds or counts too fast

ErrorThe timer goes from 60 to 59 and stops

Short answer

A countdown breaks for three reasons. The interval callback reads an old snapshot of state, so it keeps writing the same number. The effect depends on the counter and rebuilds the interval every tick. Or the effect has no cleanup, so a second interval runs. Use an updater function and return clearInterval.

  • Applies to React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log inside the effect and the callback. Count how many times the effect runs, and whether a cleanup message appears before each new run.

    useEffect(() => {
      console.log('effect start');
      const id = setInterval(() => console.log('tick'), 1000);
      return () => { console.log('effect cleanup'); clearInterval(id); };
    }, []);
  2. 2Match what you see, then jump to its fix

    1. 1setCount(count - 1) inside setInterval with [] as dependencies: the value changes once, then staysThe callback reads a stale countFix
    2. 2Seconds is in the dependency array, so the effect re-runs every second and the interval is recreatedThe effect depends on the counter and rebuilds the interval each tickFix
    3. 3The timer is correct in production but runs at double speed only in developmentNo cleanup, so Strict Mode or navigation leaves a second intervalFix

What does the error look like?

  • The timer goes from 60 to 59 and stops
  • The timer counts down twice as fast in development
  • The timer restarts its one-second wait on every render

Cause 1 of 3

The callback reads a stale count

1Check

A callback created in one render sees the state of that render only. State changes the value for the next render, not the variable the old callback already captured.

grep -n "setInterval" src/**/*.jsx

2Fix

Pass an updater function so React applies the change to the latest value.

setSeconds(s => s - 1);

3Verify

Watch the display: it now moves by one each second without a dependency on seconds.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

The effect depends on the counter and rebuilds the interval each tick

1Check

When the effect reads the counter, the counter becomes a dependency and changes every tick, so React tears down and recreates the interval each time.

grep -n "\], *\[\|}, \[" src/Timer.jsx

2Fix

Use the updater function so the effect does not read the counter, and keep the dependency array empty. Return a cleanup that clears the interval.

useEffect(() => {
  const id = setInterval(() => {
    setSeconds(s => s - 1);
  }, 1000);
  return () => clearInterval(id);
}, []);

3Verify

The console shows one effect start and no new cleanup per tick.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

No cleanup, so Strict Mode or navigation leaves a second interval

1Check

In development, Strict Mode runs setup, cleanup, setup right after mount. Without a cleanup the first interval is never cleared, so two run together. In real use the same pile-up happens each time the component is shown again.

grep -n "clearInterval" src/Timer.jsx

2Fix

Store the interval id and return a function that clears it. Do not remove StrictMode to hide the symptom.

useEffect(() => {
  const id = setInterval(() => setSeconds(s => s - 1), 1000);
  return () => clearInterval(id);
}, []);

3Verify

In development the display drops by one per second.

npm run dev

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Whenever an effect starts a timer, write its cleanup in the same edit.
  • Use updater functions for any state change that depends on the previous value.
  • Open the console in development and confirm each effect start is paired with a cleanup.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.