view js detect active tab code example
Example 1: js if tab is active
$(window).on("blur focus", function(e) {
var prevType = $(this).data("prevType");
if (prevType != e.type) {
switch (e.type) {
case "blur":
break;
case "focus":
break;
}
}
$(this).data("prevType", e.type);
})
Example 2: javascript detect if the browser tab is active
var interval_id;
$(window).focus(function() {
if (!interval_id)
interval_id = setInterval(hard_work, 1000);
});
$(window).blur(function() {
clearInterval(interval_id);
interval_id = 0;
});