Created
July 15, 2015 16:58
-
-
Save sergiogarciadev/337cda512e5daad22547 to your computer and use it in GitHub Desktop.
Detect HTML Window Focus
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
var focused = false, | |
focusedTimeout, | |
lastDate; | |
function anim() { | |
clearTimeout(focusedTimeout); | |
focused=true; | |
if (!lastDate) { | |
lastDate = new Date(); | |
} else if (new Date() - lastDate > 1000) { | |
lastDate = new Date(); | |
console.log(lastDate); | |
} | |
focusedInterval = setTimeout(function () { focused = false; }, 100); | |
requestAnimationFrame(anim); | |
} | |
setInterval(function () { | |
if (focused) { | |
console.log('Focused!'); | |
} else { | |
console.log('Not focused!'); | |
} | |
}, 1000); | |
anim() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment