Facebook Pixel

How do you handle setTimeout in background tabs?

Do not rely on timers for background work (they are throttled to ~1000ms). Use requestAnimationFrame for visual work (pauses in background). Use the Page Visibility API to detect when the tab is hidden and adjust behavior.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in setTimeout Pitfalls and How to Avoid Them 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.

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').

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0