data() {
return {
browseTime: 0, // 浏览时长初始值为 0
clearTimeSet: null,
};
},
methods: {
setTime() {
//设置定时器
this.clearTimeSet = setInterval(() => {
this.browseTime++;
console.log(this.browseTime, "时长累计");
}, 1000);
},
}
mounted() {
this.setTime(); // 页面加载完成后开始计时
},
beforeDestroy() {
clearInterval(this.clearTimeSet); // 离开页面后清除定时器
this.addBrowseRecord(); // 上送后台接口,将浏览时长等信息传到后台,离开当前路由后调用
},