Skip to content

Instantly share code, notes, and snippets.

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