Last active
June 4, 2024 08:09
-
-
Save faridv/da330ef631151f00f468af7e3ed608e7 to your computer and use it in GitHub Desktop.
Event listener to check if window/tab is active or inactive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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