-
-
Save shmup/b358eb6d75c6b46ae83e to your computer and use it in GitHub Desktop.
0
This file contains hidden or 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
class window.IdleTimer | |
constructor: () -> | |
@timer | |
@timeIncrement = 1000 | |
@timeIdle = 0 | |
@timeMax = 10000 | |
@expired = false | |
@interactionMethods = ['click','mousemove'] | |
# these should not be in here. move somewhere else. | |
@expiryCallback = () -> console.log 'expiry callback' | |
@tickCallback = () -> console.log 'tick callback' | |
@awakeCallback = () -> console.log 'awake callback' | |
if document.documentElement.hasOwnProperty('ontouchmove') | |
@interactionMethods.push('touchmove') | |
start: () -> | |
@timeIdle = 0 | |
@timer = setInterval (=> @checkIdleTime()), @timeIncrement | |
for method in @interactionMethods | |
document.addEventListener(method, => @resetTimer()) | |
stop:() -> | |
clearInterval(@timer) | |
for method in @interactionMethods | |
document.removeEventListener(method, => @resetTimer()) | |
checkIdleTime: () -> | |
@timeIdle += @timeIncrement | |
if typeof @tickCallback is 'function' and @timeIdle <= @timeMax | |
@tickCallback() | |
if @expired is off and @timeIdle > @timeMax | |
@expired = on | |
if typeof @expiryCallback is 'function' | |
@expiryCallback() | |
resetTimer:() -> | |
if @expired is on | |
@expired = off | |
if typeof @awakeCallback is 'function' | |
@awakeCallback() | |
@timeIdle = 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment