Skip to content

Instantly share code, notes, and snippets.

@sergiogarciadev
Created July 15, 2015 16:58
Show Gist options
  • Save sergiogarciadev/337cda512e5daad22547 to your computer and use it in GitHub Desktop.
Save sergiogarciadev/337cda512e5daad22547 to your computer and use it in GitHub Desktop.
Detect HTML Window Focus
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