How do you pause and resume a stopwatch in JavaScript?
On stop, add (Date.now() - startTime) to elapsed. On start (resume), set startTime = Date.now() again. The total is always elapsed + (Date.now() - startTime). This preserves the elapsed time across pause/resume.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Build a Stopwatch in an Interview
Use Date.now() for accurate timing (not interval counting). Track startTime and elapsed. Start sets startTime and runs setInterval. Stop adds the difference to elapsed. Lap records the current time. Reset clears everything. Format with leading zeros.
Because setInterval is not exact. The interval may fire late (due to event loop, throttling). Counting intervals accumulates drift. Using Date.now() calculates the actual elapsed time, which is always accurate.
Convert milliseconds to minutes, seconds, and milliseconds. Use padStart for leading zeros: String(minutes).padStart(2, '0'). Format as MM:SS.mmm or HH:MM:SS.mmm.
Still have questions?
Browse all our FAQs or reach out to our support team
