My React countdown timer sticks, skips seconds or counts too fast
The timer goes from 60 to 59 and stopsShort 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.
Fix it fast
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); }; }, []);2Match what you see, then jump to its fix
- 1
setCount(count - 1) inside setInterval with [] as dependencies: the value changes once, then staysThe callback reads a stale countFix - 2
Seconds 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
The timer is correct in production but runs at double speed only in developmentNo cleanup, so Strict Mode or navigation leaves a second intervalFix
- 1
What does the error look like?
The timer goes from 60 to 59 and stopsThe timer counts down twice as fast in developmentThe 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/**/*.jsx2Fix
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 devConfirmed by react.dev
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.jsx2Fix
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 devConfirmed by react.dev
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.jsx2Fix
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 devConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix