Last active
January 25, 2021 21:57
-
-
Save thatkookooguy/c4bc0250517dae126e69ebef3e42eee9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class inactivityCheck { | |
private timeoutId: ReturnType<typeof setTimeout>; | |
constructor( | |
public element: HTMLElement, | |
public delay: number, | |
public callbackActive: (element: HTMLElement) => void, | |
public callbackInactive: (element: HTMLElement) => void | |
) { | |
this.setupTimers(); | |
} | |
public setupTimers() { | |
this.element.addEventListener('mousemove', this.resetTimer, false); | |
this.element.addEventListener('mousedown', this.resetTimer, false); | |
this.element.addEventListener('keypress', this.resetTimer, false); | |
this.element.addEventListener('touchmove', this.resetTimer, false); | |
this.startTimer(); | |
} | |
public startTimer() { | |
this.timeoutId = setTimeout(() => this.callbackInactive(this.element), this.delay); | |
} | |
public resetTimer() { | |
this.callbackActive(this.element); | |
clearTimeout(this.timeoutId); | |
this.startTimer(); | |
} | |
public stopTimers() { | |
this.callbackActive(this.element); | |
clearTimeout(this.timeoutId); | |
this.element.removeEventListener('mousemove', this.resetTimer, false); | |
this.element.removeEventListener('mousedown', this.resetTimer, false); | |
this.element.removeEventListener('keypress', this.resetTimer, false); | |
this.element.removeEventListener('touchmove', this.resetTimer, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { inactivityCheck } from './inactivity-check'; | |
let checker; | |
function initialMouseHideOnInactive() { | |
checker = new inactivityCheck( | |
document.querySelector('#container'), | |
2000, | |
(element) => element.style.cursor = 'none', | |
(element) => element.style.cursor = null | |
); | |
} | |
function stopMouseHideOnInactive() { | |
checker.stopTimers(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment