vue polling method

// 页面挂载完成后运行轮询
mounted() {
    this.timer = window.setInterval(()=>{
        setTimeout(()=>{
	        // 轮询里的处理代码
		    this.getOrderLists();
	    },0);
    },10000);
},
methods: {
    getOrderLists() {
        //请求接口
    }
},
// 页面销毁的时候清除定时器
destroyed() {
  	window.clearInterval(this.timer);
}

1. Pay attention to the problem:
general polling will use setInterval, but using it alone will cause the page to freeze.

2. Instructions for use:
setInterval will not clear the timer queue, and every time it is executed repeatedly, the timer will be superimposed, and the web page will be stuck. But setTimeout has its own clear timer, and the combination of the two will prevent the page from being stuck.

Page initialization, after polling starts, leave the page, and destroy the scheduled task through the life cycle destroyed hook function.

Guess you like

Origin blog.csdn.net/bo_ranlove/article/details/129990667