巴青农资商城

homeRealtimePoll.js 879B

1234567891011121314151617181920212223242526272829303132
  1. /** 首页「实时」时段对应 range 参数 */
  2. export const HOME_REALTIME_RANGE = 'TODAY'
  3. const POLL_MS = 10000
  4. /**
  5. * 实时时段下每 10s 拉数;非实时或离开页面前需 clearHomeRealtimePoll
  6. * @param {object} vm 面板组件实例(挂 _homePollTimer)
  7. * @param {() => void} onTick 定时回调(建议静默刷新)
  8. */
  9. export function syncHomeRealtimePoll(vm, onTick) {
  10. clearHomeRealtimePoll(vm)
  11. if (vm.activeRange !== HOME_REALTIME_RANGE) {
  12. return
  13. }
  14. vm._homePollTimer = setInterval(() => {
  15. if (vm.activeRange !== HOME_REALTIME_RANGE) {
  16. clearHomeRealtimePoll(vm)
  17. return
  18. }
  19. onTick()
  20. }, POLL_MS)
  21. }
  22. /** 停止轮询(切换时段、离开 Tab、组件销毁时调用) */
  23. export function clearHomeRealtimePoll(vm) {
  24. if (vm._homePollTimer) {
  25. clearInterval(vm._homePollTimer)
  26. vm._homePollTimer = null
  27. }
  28. }