Last active
August 29, 2015 14:19
-
-
Save levrik/634c363c5dc6d04ec156 to your computer and use it in GitHub Desktop.
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 pollCount = 1; | |
var poll = function() { | |
if (document.hidden) // if the interval has not been cleared right | |
return; | |
var p = document.createElement('p'); | |
p.innerText = 'Polled ' + pollCount + ' times.'; | |
document.body.insertBefore(p, document.body.firstChild); | |
pollCount++; | |
}; | |
var intervalId = setInterval(poll, 1000); | |
document.addEventListener('visibilitychange', function() { | |
if (document.visibilityState === 'hidden') | |
clearInterval(intervalId); | |
else | |
intervalId = setInterval(poll, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment