Skip to content

Instantly share code, notes, and snippets.

@shinypb
Created January 19, 2011 21:39
Show Gist options
  • Save shinypb/786923 to your computer and use it in GitHub Desktop.
Save shinypb/786923 to your computer and use it in GitHub Desktop.
Test whether your browser supports window focus/blur events
<!DOCTYPE html>
<script>
var timer = startTimer();
function startTimer() {
return setInterval(function() {
console.log('tick');
}, 1000);
}
window.onblur = function() {
if(timer) {
console.log('stopping');
clearInterval(timer);
timer = null;
}
}
window.onfocus = function() {
if(!timer) {
console.log('starting');
timer = startTimer();
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment