1. Usage scenarios There is such a requirement, so a system was developed. When the user leaves the desktop or does not operate for a period of time, all open pages of the system need to be locked, just like the desktop lock screen. Only after successfully entering the password for verification or logging in again can the page be continued to be operated. If the page is refreshed, it must remain locked. Just like the picture below. Of course, users can also trigger the lock screen manually. The purpose is to prevent others from arbitrarily accessing important content of the system. So how do we achieve this? The 5s lock screen effect is as follows: 2. Ideas
It's a bit confusing and needs to be sorted out. 3. Code Implementation The following code is incomplete, the HTML structure is omitted, and you can use it freely. // app.vue data () { return { timeOut: 5000, timer: null, isLock: 'false' } }, mounted () { this.timer = setTimeout(this.lockPro, this.timeOut) // Set the operation time for the first time localStorage.setItem('moveTime', Date.now()) //First judgement status this.modalStatus() // Poll monitoring status setInterval(this.modalStatus, 1000) // Listen for mouse events this.events() }, methods:{ events() { window.onmousemove = () => { // console.log('The mouse has moved') if (!this.isLock) { localStorage.setItem('moveTime', Date.now()) this.clearLocaPro('continue') } } }, modalStatus() { if (localStorage.getItem('isLock') === 'true') { // console.log('Screen locked') this.isLock = true this.clearLocaPro() } else { // console.log('The screen is not currently locked') this.isLock = false this.clearLocaPro('continue') } }, lockPro() { if (!this.timeOut) { localStorage.setItem('isLock', 'false') this.clearLocaPro('continue') return } if (Date.now() - localStorage.getItem('moveTime') < this.timeOut) { localStorage.setItem('isLock', 'false') this.clearLocaPro('continue') } else { localStorage.setItem('isLock', 'true') this.clearLocaPro() } }, clearLocaPro(status) { if(this.timer){ clearTimeout(this.timer) } if (status === 'continue') { this.timer = setTimeout(this.lockPro, this.timeOut) } }, // Manual lock handleLock(){ this.clearLocaPro() localStorage.setItem('isLock', 'true') }, // Password unlock unlock(){ localStorage.removeItem('isLock') localStorage.setItem('moveTime', Date.now()) this.clearLocaPro('continue') }, ... // Don't forget to clear isLock when logging out } This is the end of this article about how to implement automatic screen lock using js. For more information about automatic screen lock using js, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL SQL statement to find duplicate data based on one or more fields
>>: How to install golang under linux
I believe everyone is very sensitive to colors. C...
var numA = 0.1; var numB = 0.2; alert( numA + num...
Table of contents 1. Use SVG 2. Use fontAwesome 3...
1. Experimental Environment serial number project...
Table of contents Structural inheritance (impleme...
Step 1: Add a secondary domain name to the Alibab...
Installing Electron cnpm install electron -g Inst...
Remax is an open source framework developed by An...
This article example shares the specific code of ...
Setting min-width and max-width properties in tab...
Table of contents 1. What is lazy loading of rout...
This article shares the installation and configur...
Table of contents Docker Installation Nvidia-dock...
When using docker images, images with both REPOSI...
MySQL green version setting code, and 1067 error ...