What are common setTimeout pitfalls in JavaScript?
Expecting exact timing, lost this binding, the closure loop bug (var vs let), forgetting to clear timers, passing arguments incorrectly (calling instead of passing), blocking the stack, and background tab throttling.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in setTimeout Pitfalls and How to Avoid Them in JavaScript
Use .bind(obj) to permanently bind this, or wrap the method call in an arrow function (() => obj.method()). Arrows inherit this lexically, so they preserve the correct binding.
Because console.log('hi') is called immediately (it returns undefined), and undefined is passed to setTimeout. Fix by passing the function reference: setTimeout(() => console.log('hi'), 1000) or setTimeout(console.log, 1000, 'hi').
Always store the timer ID and call clearTimeout or clearInterval when done. In React, clean up in useEffect: return () => clearInterval(id). In component-based frameworks, clean up when the component unmounts.
Still have questions?
Browse all our FAQs or reach out to our support team
