Skip to content

Instantly share code, notes, and snippets.

@thatkookooguy
Last active January 25, 2021 21:57
Show Gist options
  • Save thatkookooguy/c4bc0250517dae126e69ebef3e42eee9 to your computer and use it in GitHub Desktop.
Save thatkookooguy/c4bc0250517dae126e69ebef3e42eee9 to your computer and use it in GitHub Desktop.
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);
}
}
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