Skip to content

Instantly share code, notes, and snippets.

@levrik
Last active August 29, 2015 14:19
Show Gist options
  • Save levrik/634c363c5dc6d04ec156 to your computer and use it in GitHub Desktop.
Save levrik/634c363c5dc6d04ec156 to your computer and use it in GitHub Desktop.
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