congming_huose-apk/common/mixins/deviceDetailPoll.js

44 lines
1.0 KiB
JavaScript

/**
* 设备详情页:进入/回到页面时启动定时器,每 5s 调用 getxq() 拉取详情;
* 隐藏、卸载或切换离开页面时清除定时器。
*/
const POLL_INTERVAL_MS = 5000
export default {
data() {
return {
_deviceDetailPollTimer: null,
}
},
onShow() {
this._startDeviceDetailPoll()
},
onHide() {
this._stopDeviceDetailPoll()
},
onUnload() {
this._stopDeviceDetailPoll()
},
onBeforeUnmount() {
this._stopDeviceDetailPoll()
},
methods: {
_startDeviceDetailPoll() {
this._stopDeviceDetailPoll()
if (typeof this.getxq !== 'function') return
if (this.id === '' || this.id === null || this.id === undefined) return
this._deviceDetailPollTimer = setInterval(() => {
if (typeof this.getxq !== 'function') return
if (this.id === '' || this.id === null || this.id === undefined) return
this.getxq()
}, POLL_INTERVAL_MS)
},
_stopDeviceDetailPoll() {
if (this._deviceDetailPollTimer != null) {
clearInterval(this._deviceDetailPollTimer)
this._deviceDetailPollTimer = null
}
},
},
}