fix: document.addEventListener visibilitychange window.addEventListener pagehide

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-02-09 17:42:03 +07:00
parent 5d01f4d400
commit 13ac203c62
2 changed files with 49 additions and 33 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { ref, reactive, onMounted, watch, onBeforeUnmount } from 'vue'
import { useQuasar } from 'quasar'
import moment from 'moment'
import Camera from 'simple-vue-camera'
@ -504,27 +504,37 @@ const getClassXS = (val: boolean) => {
}
const inQueue = ref<boolean>(false)
const photoTimeout = ref<any | null>(null)
const PHOTO_TIMEOUT_DURATION = 5 * 60 * 1000 // 5
// timeout
function startPhotoTimeout() {
clearPhotoTimeout() // timeout ()
photoTimeout.value = setTimeout(() => {
// 5
if (img.value) {
img.value = undefined
cameraIsOn.value = false
camera.value?.stop()
}
}, PHOTO_TIMEOUT_DURATION)
//
function resetCameraAndImage() {
if (img.value) {
img.value = undefined
}
if (cameraIsOn.value && camera.value) {
camera.value.stop()
cameraIsOn.value = false
}
}
function clearPhotoTimeout() {
if (photoTimeout.value) {
clearTimeout(photoTimeout.value)
photoTimeout.value = null
//
function handleAppClose() {
resetCameraAndImage()
// interval
if (intervalId.value !== undefined) {
clearInterval(intervalId.value)
intervalId.value = undefined
}
}
// visibility change
function handleVisibilityChange() {
if (document.visibilityState === 'hidden') {
handleAppClose()
} else if (document.visibilityState === 'visible') {
//
if (privacyStore.isAccepted) {
mapRef.value?.requestLocationPermission()
}
}
}
@ -538,6 +548,24 @@ onMounted(async () => {
if (privacyStore.isAccepted) {
mapRef.value?.requestLocationPermission()
}
// event listeners
document.addEventListener('visibilitychange', handleVisibilityChange)
window.addEventListener('pagehide', handleAppClose)
})
onBeforeUnmount(() => {
resetCameraAndImage()
// interval
if (intervalId.value !== undefined) {
clearInterval(intervalId.value)
intervalId.value = undefined
}
// event listeners
document.removeEventListener('visibilitychange', handleVisibilityChange)
window.removeEventListener('pagehide', handleAppClose)
})
watch(