Facebook Pixel

setTimeout Interview Questions: Trust Issues in JavaScript

setTimeout timing is a favorite interview topic. Here are the most asked questions about its trustworthiness.

setTimeout Interview Questions: Trust Issues in JavaScript

setTimeout timing is a favorite interview topic. Here are the most asked questions.

Q1: Does setTimeout(cb, 1000) guarantee exact 1000ms?

No. It guarantees a minimum of 1000ms. The actual delay depends on the call stack, microtasks, background tab throttling, and nested timer clamping.

Q2: What is the output?

setTimeout(() => console.log("timer"), 0); for (let i = 0; i < 1e9; i++) {}

Answer: "timer" logs after 5 seconds. The callback was queued; the event loop could not run it while the loop was blocking the stack.

Q3: What is the minimum delay for nested setTimeout?

4ms after 5 nested levels (HTML spec).

Q4: Does setTimeout work normally in background tabs?

No. Browsers throttle timers in background tabs to ~1000ms minimum.

Q5: What is the output?

console.log("start"); setTimeout(() => console.log("timeout"), 0); Promise.resolve().then(() => console.log("promise")); console.log("end");

Answer: start, end, promise, timeout. Microtasks before macrotasks.

Q6: Can setTimeout callbacks run before the delay?

No. The callback never runs before the specified delay. It may run later, but never earlier.

Q7: What happens if you pass a negative delay?

The delay is treated as 0. The callback goes to the macrotask queue.

Q8: How do you cancel a setTimeout?

const id = setTimeout(cb, 1000); clearTimeout(id);

The Takeaway

setTimeout interview questions test: the minimum-delay guarantee, blocking the stack, FIFO ordering, nested timer clamping (4ms), background tab throttling, microtasks before macrotasks, negative delay (treated as 0), and clearTimeout.

No. It guarantees a minimum of 1000ms. The actual delay depends on the call stack, microtask queue, background tab throttling, and nested timer clamping. The callback runs at least after 1000ms, but often later.

4ms after 5 nested levels. The HTML spec specifies this to prevent abuse. The first 5 levels can use 0ms, but after that, the delay is clamped to 4ms.

No. The callback never runs before the delay. It may run later (if the stack is busy, microtasks are queued, or the tab is in the background), but never earlier.

The delay is treated as 0. The callback goes to the macrotask queue and runs after the call stack is empty and all microtasks are done.

Use a self-correcting timer: measure the drift with Date.now() or performance.now(), and adjust the next delay to compensate. Or use requestAnimationFrame for visual intervals.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.