A recent business involves such a requirement that the online training system needs to know the user's preview time for a certain online preview page. Initially, we first thought of using the life cycle functions of the Vue page, With an idea in mind, we can start planning the specific code. Define the start and end timing functions In export default { data() { return { timer: null, duration: 0 } }, methods: { startTimer() { this.timer = setInterval(() => { console.log('Watching time: ', this.duration) this.duration++ }, 1000) }, stopTimer() { clearInterval(this.timer) this.updateViewHistory() // Report data interface}, updateViewHistory() { // Reporting interface logic code.... } } } In the How and where to call After defining the start and end methods, we need to start thinking about where to call them. Because the preview page content is not unique, it is rendered based on the material's So I chose to listen to the watch: $route: { immediate: true, handler(to, from) { if (to.params.id) this.trainingId = to.params.id this.startTimer() } } } The start timing method is called, and finally we can see the current duration output in Then, as the last step, we need to call
mounted() { window.addEventListener('beforeunload', e => this.beforeunloadHandler(e)) }, destroyed() { window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e)) } Indirectly call methods: { beforeunloadHandler (e) { this.stopTimer() } } Some people may ask why not call When writing code, we not only have to implement the function, but also think more. This is the difference between ordinary people and experts. This is the end of this article about Vue page monitoring user preview time. For more relevant Vue monitoring user preview time content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 5.7.19 winx64 free installation version configuration tutorial
>>: How to delete special character file names or directories in Linux
1. View the detailed information of all current c...
Here's the thing: Everyone knows about "...
Previously, react.forwardRef could not be applied...
MySQL full-text index is a special index that gen...
Differences between Docker and Docker Machine Doc...
Preface A character set is a set of symbols and e...
Preface: During database operation and maintenanc...
Preview of revised version This article was writt...
Use CSS3 to achieve cool radar scanning pictures:...
Reasonable setting of MySQL sql_mode sql_mode is ...
1. Environment Ubuntu 16.04 running on a virtual ...
Table of contents Vue first screen performance op...
title XML/HTML CodeCopy content to clipboard <...
Table of contents background Problem Description ...
As we all know, the web pages, websites or web pag...