Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Sharik M
Courses Plus Student 2,217 PointsJavaScript: Need to Create a timer in Popup window which stops when the window is minimized.
Hello Everyone!! I need to create a timer box in a popup window which will run only when the window is active and will stop when the window is minimized/not active, can anyone please help me in doing the same.
2 Answers

Steven Parker
242,796 PointsYou may be able to use the visibilitychange event.
I'm guessing you already know how to manage your timer and you only need to know when the window is minimized and restored.
You can create an event handler for the "visibilitychange" event on the document, and then test the boolean property document.hidden to see what kind of change occurred. But bear in mind that minimizing is not the only cause of being "hidden", switching browser tabs would do the same thing (but perhaps you want to react the same way them also).

Sharik M
Courses Plus Student 2,217 PointsJust an update,I resolved it by using window.onfocus() and window.blur() events which are already part of JavaScript event library.

Steven Parker
242,796 PointsThat's great if it works for you, but that's not quite the same thing.
The blur event will fire anytime you click outside of the window, such as to another window even if it does not obscure the first one. And at that point it will not fire when you minimize the window. There are similar differences in the "focus" behavior.
I'm not sure what you mean by "already part of JavaScript event library", isn't "visibilitychange" also? But be sure to use it with addEventListener as the ononvisibilitychange property may not be widely browser-supported.
Sharik M
Courses Plus Student 2,217 PointsSharik M
Courses Plus Student 2,217 PointsThanks Steve, i think this event should be able to resolve my issue, ill keep you guys posted...:-)Cheers!!