In modern browser, from IE10 and up, you can use the Visibility API to figure out if the tab is active or not, and only keep polling in an active tab, something like this
var hidden, visibilityChange, timer;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
function handleVisibilityChange() {
if (document[hidden]) {
stopAjax();
} else {
startAjax();
}
}
document.addEventListener(visibilityChange, handleVisibilityChange, false);
function startAjax() {
timer = setInterval(function() {
$.ajax(options);
}, 4000);
}
function stopAjax() {
clearInterval(timer);
}