Created
April 26, 2014 11:44
-
-
Save thischarmingsam8/11317970 to your computer and use it in GitHub Desktop.
Coffeescript version of the simple page idle timer.
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'] | |
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