Say I have two separate browser windows open:
function onload() {
setTimeout("dosomething", 2000);
}
function dosomething() {
$.ajax({
url: "someurl",
success: function (data) {
document.write("Hello");
}
});
setTimeout("dosomething", 2000);
}
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.
How do I accomplish this?
This is tricky one, and depend on browser.