Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created May 30, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makaroni4/cd7dbdd06222de118040 to your computer and use it in GitHub Desktop.
Save makaroni4/cd7dbdd06222de118040 to your computer and use it in GitHub Desktop.
Hidden counter
# Adapted slightly from Sam Dutton
# Set name of hidden property and visibility change event
# since some browsers only offer vendor-prefixed support
hidden = undefined
state = undefined
visibilityChange = undefined
if typeof document.hidden isnt "undefined"
hidden = "hidden"
visibilityChange = "visibilitychange"
state = "visibilityState"
else if typeof document.mozHidden isnt "undefined"
hidden = "mozHidden"
visibilityChange = "mozvisibilitychange"
state = "mozVisibilityState"
else if typeof document.msHidden isnt "undefined"
hidden = "msHidden"
visibilityChange = "msvisibilitychange"
state = "msVisibilityState"
else if typeof document.webkitHidden isnt "undefined"
hidden = "webkitHidden"
visibilityChange = "webkitvisibilitychange"
state = "webkitVisibilityState"
setHiddenCounter = (start_hidden_time) ->
time = new Date().getTime()
timestamp = (time - start_hidden_time) / 1000
if $(".hidden_timestamp").val() != ""
$(".hidden_timestamp").val(parseInt($(".hidden_timestamp").val()) + timestamp)
else
$(".hidden_timestamp").val(timestamp)
# Add a listener that constantly changes the title
hiddent_counter_interval_id = undefined
document.addEventListener visibilityChange, (->
if document[state] == "hidden"
hiddent_counter_interval_id = new Date().getTime()
else
setHiddenCounter(hiddent_counter_interval_id)
hiddent_counter_interval_id = undefined
), false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment