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
I don't know if you have used the frameset at...
1. Use absolute positioning and margin The princi...
Implementation ideas: First of all, the alarm inf...
The storage size and range of each floating point...
1. Oracle is a large database while MySQL is a sm...
When using docker, you may find that the file can...
This article example shares the specific code of ...
This article example shares the specific code of ...
1. When the mobile terminal processes the list sl...
The default ssh port number of Linux servers is g...
yum install httpd php mariadb-server –y Record so...
Table of contents What is virtual dom? Why do we ...
Assuming business: View the salary information of...
This article introduces an example of how to use ...
No matter you are installing Windows or Linux ope...