DEV Community

Discussion on: How to Create a Simple React Countdown Timer

Collapse
 
elramus profile image
Luke Ramus • Edited

Cool, thank you! Only flaw is that if the user switches to a different tab, the timer pauses. Any thoughts on how to get around that, or is that just a limitation of how JavaScript runs in the browser?

Collapse
 
trangcongthanh profile image
Thành Trang • Edited

Whats limitation?

If your component will unmount, you should lift your count state up (or make it global) to store the current count. Then when remount, init your count state with the previous count. It'll start counting at the previous position.

If you component will not unmount, just clear your current timeout when the tab lost focus. It'll pause. When it get focused again, set your timeout back.