1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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

Avoid making requests to the on demand reloader if the page is not visible.
This commit is contained in:
Kevin Decker 2017-09-08 16:26:13 -05:00 committed by Tim Neutkens
parent c0d031d90e
commit 808c662e3b

View file

@ -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)
})
} }
} }
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
runPinger()
} else {
clearTimeout(pingerTimeout)
}
}, false)
setTimeout(() => {
runPinger() runPinger()
.catch((err) => { .catch((err) => {
console.error(err) console.error(err)
}) })
}, 10000)
} }