How to tell if browser/tab is active
<p>I have a function that is called every second that I only want to run if the current page is in the foreground, i.e. the user hasn't minimized the browser or switched to another tab. It serves no purpose if the user isn't looking at it and is potentially CPU-intensive, so I don't want to just waste cycles in the background.</p>

<p>Does anyone know how to tell this in JavaScript?</p>

<p>Note: I use jQuery, so if your answer uses that, that's fine :).</p>
