I think to check if the tab is active, only with pro browser plugins, but alternatively, you can use the events of focus
window:
var interval_id;
$(window).focus(function() {
if (!interval_id)
interval_id = setInterval(hard_work, 1000);
});
$(window).blur(function() {
clearInterval(interval_id);
interval_id = 0;
});
As it is said in that OS response in English.
Another alternative is to use the visibility API, which works more or less as follows:
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
Use:
var visible = vis(); // dá o estado atual da aba
vis(aFunction); // define um event handler para a troca de visibilidade