Windows store app: detecting if your app is visible or not

I wanted to find out if my game app was visible or not because I wanted to pause the game if the user Alt-Tabbed or pressed the windows key. I found the following article on stack overflow that explained that you can listen to the msvisibilitychange event to detect changes like this:

document.addEventListener("msvisibilitychange", function ()
    //Pause function

However Microsoft doesn’t seem to be very happy with you putting to much code in this event as it might cause a “less fluid user experience”. I just pause the game there though 🙂