HomeInterview QuestionsExplain how setTimeout and setInterval work in Jav…

Explain how setTimeout and setInterval work in JavaScript.

🟢 Easy Conceptual Fresher level
1Times asked
May 2026Last seen
May 2026First seen

💡 Model Answer

Both setTimeout and setInterval are timer functions that schedule callbacks to run after a specified delay. They are part of the Web APIs and use the browser’s timer queue. When you call setTimeout(fn, ms), the function fn is queued to run once after at least ms milliseconds. setInterval(fn, ms) queues fn to run repeatedly every ms milliseconds.

Under the hood, the event loop checks the timer queue after each task. When a timer expires, its callback is pushed onto the task queue. The event loop then processes the task queue, executing callbacks one by one. Because timers are not guaranteed to fire exactly on time, the actual execution may be delayed by other tasks.

Key differences:

  • setTimeout runs once; setInterval runs repeatedly.
  • setInterval can drift if the callback takes longer than the interval.
  • Both return a numeric ID that can be passed to clearTimeout or clearInterval to cancel the scheduled work.

Understanding these mechanics helps avoid bugs such as overlapping intervals or memory leaks from forgotten timers.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500