timer
Step 1: Define the timer variable (if the timer needs to start and end in different functions, you need to define a global variable)
data: {
setTime: null, //定时器
}
Step 2: Define the start timer function
startCount : function(){
this.data.setTime= setInterval(
this.countTime, 1000);
},
Step 3: Define the function that the timer needs to execute regularly
countTime: function () {
//TODO
}
Step 4: Start the timer in an event, then the countTime function will be executed regularly
myFn: function () {
this.startInter();
},
Step Five: Clear the Timer
clearInterval(this.data.setTime)
When the applet page exits, the timer and persistent connection will not be cleared and disconnected automatically, so if the timer is not cleared in the page, the timer needs to be cleared manually in the onUnload function of the life cycle.
onUnload: function () {
//结束定时器
clearInterval(this.data.setTime)
},
(Switching pages in vue also requires manually clearing the timer in the lifecycle function beforeDestroy).
beforeDestroy() {
clearInterval(this.setTime)
this.set = null;
},