Skip to content

Instantly share code, notes, and snippets.

@faridv
Last active June 4, 2024 08:09
Show Gist options
  • Save faridv/da330ef631151f00f468af7e3ed608e7 to your computer and use it in GitHub Desktop.
Save faridv/da330ef631151f00f468af7e3ed608e7 to your computer and use it in GitHub Desktop.
Event listener to check if window/tab is active or inactive
$(window).on("blur focus", function (e) {
var prevType = $(this).data("prevType");
if (prevType != e.type) { // reduce double fire issues
switch (e.type) {
case "blur":
// do work
console.log('blurred');
break;
case "focus":
// do work
console.log('activated');
break;
}
}
$(this).data("prevType", e.type);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment