Last active
March 27, 2022 11:01
-
-
Save jSayal/fbf9aef54a2a6087494b0d7273bdd5a9 to your computer and use it in GitHub Desktop.
Dealing With Browser's Restored Tabs with Visibility API
This file contains hidden or 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
| <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