Maaz Zindani

Loading the index…

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

setTimeout in a React component fires at the wrong time or never

ErrorThe callback runs immediately instead of after the delay

Short answer

setTimeout needs a function to call later. Most React cases are one of four things: you called the function instead of passing it, the callback reads old state, an effect never cleared its timer, or the browser delays timers in a background tab. Find which one by logging inside the callback.

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

Fix it fast

  1. 1Find out which cause you have

    Log a timestamp when you schedule the timer and another one inside the callback. The gap shows whether the delay is skipped, correct or stretched.

    console.log('scheduled', Date.now());
    setTimeout(() => console.log('ran', Date.now()), 2000);
  2. 2Match what you see, then jump to its fix

    1. 1The code runs the moment the component renders, not after the delayThe function is called, not passedFix
    2. 2The callback runs twice in development, or after you left the screenThe timer keeps firing after the component changes or unmountsFix
    3. 3The timer sets or shows a value that is one step behindThe callback reads a state value from an earlier renderFix
    4. 4The timer is correct in the foreground tab but late when the tab is hiddenThe browser delays timersFix
    5. 5TypeError: Cannot read properties of undefinedA class method loses this inside the timerFix

What does the error look like?

  • The callback runs immediately instead of after the delay
  • The timer uses an old state value
  • The timer fires twice in development
  • A timer in a background tab runs much later than the delay

Cause 1 of 5

The function is called, not passed

1Check

Look at the first argument of setTimeout. A name followed by parentheses runs the function now and hands its return value to the timer. MDN's examples pass the function itself.

grep -rn "setTimeout(" src/

2Fix

Pass the function, or wrap the call in an arrow function when you need arguments.

// wrong: runs now
setTimeout(hideBanner(id), 3000);

// right
setTimeout(() => hideBanner(id), 3000);

3Verify

The log shows the 'ran' time about three seconds after 'scheduled'.

console.log(Date.now())

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 5

The timer keeps firing after the component changes or unmounts

1Check

Strict Mode in development runs one extra setup and cleanup cycle for effects, so an effect that starts a timer without cleaning it up starts it twice.

grep -rn "setTimeout" src/ | grep -v clearTimeout

2Fix

Keep the timer id inside the effect and clear it in the cleanup function.

useEffect(() => {
  const id = setTimeout(() => setVisible(false), 3000);
  return () => clearTimeout(id);
}, []);

3Verify

In development the callback now fires once, and nothing happens after you navigate away.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 5

The callback reads a state value from an earlier render

1Check

A callback captures the values of the render that created it. If the effect lists the value as a dependency, the timer restarts on every change.

grep -rn "setCount(count" src/

2Fix

Use an updater function so the callback does not need the value in its closure.

useEffect(() => {
  const id = setTimeout(() => setCount(c => c + 1), 1000);
  return () => clearTimeout(id);
}, []);

3Verify

The counter increases by one each time the timer fires.

console.log('tick')

Confirmed by react.dev

Did this fix it?

Cause 4 of 5

The browser delays timers

1Check

The delay is not exact: it may be longer than set. Browsers enforce a minimum delay in inactive tabs, and Chrome checks throttled timers once per second, or once per minute with intensive throttling.

document.visibilityState

2Fix

Do not use timers for exact time. Store a start time and compute the elapsed time when the callback or the tab visibility changes.

const start = Date.now();
setTimeout(() => {
  const elapsed = Date.now() - start;
  console.log(elapsed);
}, 1000);

3Verify

The elapsed value is logged and your UI uses it, not an assumed delay.

console.log(document.visibilityState)

Confirmed by developer.mozilla.org

Did this fix it?

Cause 5 of 5

A class method loses this inside the timer

1Check

A non-arrow function passed to setTimeout runs with normal call semantics, so this is not your component or object.

grep -rn "setTimeout(this\." src/

2Fix

Wrap the call in an arrow function or bind the method.

setTimeout(() => this.refresh(), 1000);
// or
setTimeout(this.refresh.bind(this), 1000);

3Verify

The method runs without the TypeError after the delay.

console.log('refreshed')

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Always keep the id from setTimeout in an effect and clear it in the cleanup.
  • Use updater functions for state changes inside timers.
  • Do not rely on timers for exact timing; compute elapsed time from Date.now().

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.