Vue/Nuxt设置$nextTick或setTimeout来调用特定页面中的函数



我试图在页面上每10秒调用一个函数,但当我访问页面并离开它时,函数会继续运行。

mounted() {
if(this.user_authenticated) {
this.$nextTick(function () {
window.setInterval(() => {
this.updateValuePage();
},10000);
})
}
}

如果我离开页面,函数将继续运行。如何仅在该页面中而不在全局范围内停止或使用它?我也尝试过创建((,但问题相同。该函数在页面本身中,而不是在mixin或插件中。

您需要使用clearInterval((清除间隔

let internal;
export default {
mounted() {
if(this.user_authenticated) {
this.$nextTick(function () {
interval = window.setInterval(() => {
this.updateValuePage();
},10000);
})
}
}, 
beforeDestroy() {
clearInternval(interval);
} 
}

最新更新