Skip to content

Instantly share code, notes, and snippets.

@jSayal
Last active March 27, 2022 11:01
Show Gist options
  • Save jSayal/fbf9aef54a2a6087494b0d7273bdd5a9 to your computer and use it in GitHub Desktop.
Save jSayal/fbf9aef54a2a6087494b0d7273bdd5a9 to your computer and use it in GitHub Desktop.
Dealing With Browser's Restored Tabs with Visibility API
<script>
const TIME_TO_STALE = 12 * 60 * 60 * 1000; // 12 hours in milliseconds
window.onload = () => {
localStorage.setItem('lastLoadTime', Date.now());
};
// Refresh website after 12 hours
document.addEventListener('visibilitychange', () => {
const currentTime = Date.now().getTime();
const lastLoadTime = +(localStorage.getItem('lastLoadTime')) || Date.now();
// Reload - if content is stale
if (!document.hidden && currentTime - lastLoadTime >= TIME_TO_STALE) {
location.reload();
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment