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
This article describes the deployment method of A...
Environment: VMware VCSA 6.7 (VMware-VCSA-all-6.7...
In MySQL, create a view on two or more base table...
Table of contents Docker images What is a mirror?...
The full name of Blog should be Web log, which me...
Preface Mobile devices have higher requirements f...
Primary Key: Keyword: primary key Features: canno...
The so-called sliding door technology means that ...
Table of contents 1. Project Requirements Second,...
When you see a class, what information do you wan...
I personally feel that the development framework ...
Nginx, pronounced "engine x," is an ope...
The key is that the local server does not have wr...
MySQL replace and replace into are both frequentl...
Isolation of process address spaces is a notable ...