SoFunction
Updated on 2025-04-05

How to change the polling status in vue project (hook function)

Polling status changes in vue project

In actual projects, if you do not use websoc, you need to use polling. For polling, it is actually a timer set by the front-end, and continuously store and obtain data and make changes.

After exiting this interface, the polling logic is still performed during the execution of the timer. At this time, you need to use the hook function to determine the route's departure status and clear the timer

 //Leave the current pagebeforeRouteLeave(to, from, next) {
    ();

    //Clear the timer    next();
},

vue polling method and clearing

<script>
  var Vue = new Vue({
    el: '#app',
    data: {
      timer: null,
    },
    created() {
		()
    },
    methods: {
      //polling      pollfun() {
         = (() => {
          setTimeout(() => {
            ()
          }, 0)
        }, 3000)
      },
      //Clear poll      clearfun() {
        clearInterval();
         = null;
      }
    },
    //Clear the page    destroyed() {
      ()
    }
  })
</script>

destroy is the kill hook function of the page

The above is personal experience. I hope you can give you a reference and I hope you can support me more.