Skip to content

Commit

Permalink
feat: if page hidden, stop polling
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Mar 11, 2023
1 parent e814686 commit b7438d3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/core/src/injectScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let hasShowSystemUpdateNotice = false
/** latest version from server */
let latestVersion = ''
let currentLocale = ''
let intervalTimer: NodeJS.Timer | undefined

/**
* limit function
Expand Down Expand Up @@ -66,16 +67,25 @@ function checkUpdate(options: Options) {
// check system update after page loaded
setTimeout(checkSystemUpdate)

// polling check system update
if (checkInterval > 0)
setInterval(checkSystemUpdate, checkInterval)
/**
* polling check system update
*/
const pollingCheck = () => {
if (checkInterval > 0)
intervalTimer = setInterval(checkSystemUpdate, checkInterval)
}
pollingCheck()

const limitCheckSystemUpdate = limit(checkSystemUpdate, 5000)

// when page visibility change, check system update
window.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible')
if (document.visibilityState === 'visible') {
pollingCheck()
limitCheckSystemUpdate()
}
if (document.visibilityState === 'hidden')
intervalTimer && clearInterval(intervalTimer)
})

// when page focus, check system update
Expand Down

0 comments on commit b7438d3

Please sign in to comment.