Skip to content

Instantly share code, notes, and snippets.

@HiroshiOkada
Last active October 2, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HiroshiOkada/6344f094955f3fc31deeffeec2718ff6 to your computer and use it in GitHub Desktop.
Save HiroshiOkada/6344f094955f3fc31deeffeec2718ff6 to your computer and use it in GitHub Desktop.
poor man's autoreload
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