js實現自動鎖屏功能
有這么一個需求,開發了一套系統,當用戶離開桌面或者一段時間不操作的話,需要把該系統所有打開頁面鎖定起來,就跟桌面鎖屏一樣,只能輸入密碼驗證成功后,或者重新登錄,才可以繼續操作頁面,如果刷新頁面,也要保持鎖定。就像下圖一樣。當然用戶也可以手動觸發鎖屏。目的是防止他人隨意操作系統的重要內容。那么該如何去實現呢?
5s鎖屏效果如下:
有點繞,需要好好捋一捋。
3.代碼實現以下代碼是不完全代碼,html結構省略了,大家自由發揮。
// app.vuedata () { return { timeOut: 5000, timer: null, isLock: ’false’ }},mounted () { this.timer = setTimeout(this.lockPro, this.timeOut) // 首次設置操作時間 localStorage.setItem(’moveTime’, Date.now()) // 首次判斷狀態 this.modalStatus() // 輪詢監聽狀態 setInterval(this.modalStatus, 1000) // 監聽鼠標事件 this.events()},methods:{ events() { window.onmousemove = () => {// console.log(’鼠標移動了’)if (!this.isLock) { localStorage.setItem(’moveTime’, Date.now()) this.clearLocaPro(’continue’)} } }, modalStatus() { if (localStorage.getItem(’isLock’) === ’true’) {// console.log(’鎖屏了’)this.isLock = truethis.clearLocaPro() } else {// console.log(’當前沒鎖屏’)this.isLock = falsethis.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) } }, // 手動鎖定 handleLock(){ this.clearLocaPro() localStorage.setItem(’isLock’, ’true’) }, // 密碼解鎖 unlock(){ localStorage.removeItem(’isLock’) localStorage.setItem(’moveTime’, Date.now()) this.clearLocaPro(’continue’) }, ... // 別忘了退出登錄也要清除isLock}
到此這篇關于js實現自動鎖屏功能的文章就介紹到這了,更多相關js 自動鎖屏 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
