Last active
October 2, 2018 05:37
-
-
Save HiroshiOkada/6344f094955f3fc31deeffeec2718ff6 to your computer and use it in GitHub Desktop.
poor man's autoreload
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
window.addEventListener("load", () => | |
(async () => { | |
const interval = 800 | |
const duration = 10 * 60 * 1000 | |
const option = { credentials: 'include' } | |
const response = await fetch(location.href, option) | |
if (!response.ok) { | |
return | |
} | |
const orgText = await response.text() | |
const t0 = Date.now() | |
while (Date.now() - t0 < duration) { | |
await new Promise(resolve => setTimeout(resolve, interval)) | |
const response = await fetch(location.href, option) | |
if (response.ok) { | |
const text = await response.text() | |
if (orgText !== text) { | |
location.reload(true) | |
return | |
} | |
} | |
} | |
})() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment