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.

Revisions

  1. jSayal revised this gist Mar 27, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    };

    // Refresh website after 12 hours
    document.addEventListener('visibilitychange', function() {
    document.addEventListener('visibilitychange', () => {
    const currentTime = Date.now().getTime();
    const lastLoadTime = +(localStorage.getItem('lastLoadTime')) || Date.now();

  2. jSayal created this gist Mar 27, 2022.
    18 changes: 18 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <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', function() {
    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>