Commit 808c662e authored by Kevin Decker's avatar Kevin Decker Committed by Tim Neutkens

Tie page visibility api to on demand pinger (#2818)

Avoid making requests to the on demand reloader if the page is not visible.
parent c0d031d9
...@@ -30,15 +30,31 @@ export default () => { ...@@ -30,15 +30,31 @@ export default () => {
} }
} }
let pingerTimeout
async function runPinger () { async function runPinger () {
while (true) { // Will restart on the visibilitychange API below. For older browsers, this
await new Promise((resolve) => setTimeout(resolve, 5000)) // will always be true and will always run, but support is fairly prevalent
// at this point.
while (!document.hidden) {
await ping() await ping()
await new Promise((resolve) => {
pingerTimeout = setTimeout(resolve, 5000)
})
} }
} }
runPinger() document.addEventListener('visibilitychange', () => {
.catch((err) => { if (!document.hidden) {
console.error(err) runPinger()
}) } else {
clearTimeout(pingerTimeout)
}
}, false)
setTimeout(() => {
runPinger()
.catch((err) => {
console.error(err)
})
}, 10000)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment