Javascript execute timer function only if window is in focus?
<p>Say I have two separate browser windows open:</p>

<pre><code>function onload() {
    setTimeout("dosomething", 2000);
}

function dosomething() {
    $.ajax({
        url: "someurl",
        success: function (data) {
            document.write("Hello");
        }
    });
    setTimeout("dosomething", 2000);
}
</code></pre>

<p>I want to make it so that hello is printed every two seconds only when the window is in focus. If the window is not in focus, then the timer would essentially pause (in other words, timer would stop ticking, dosomething would not be called) until the user clicks on the browser window that is not in focus, which the timer would resume and the Hello words would continue to be printed, while the other window would stop timer, vice versa.</p>

<p>How do I accomplish this?</p>
