Skip to content

Instantly share code, notes, and snippets.

@raininglemons
Created August 24, 2015 20:14
Show Gist options
  • Save raininglemons/443f9def07799a1fc7aa to your computer and use it in GitHub Desktop.
Save raininglemons/443f9def07799a1fc7aa to your computer and use it in GitHub Desktop.
Idler, minified to 1.53 KB, or just 613 bytes with Gzip compression.
!function(){"use strict";var e=function(n,i,t){t=t||6e4,this.onIdle=n,this.onInteraction=i,this.idleTime=t,this.isIdle=!1,e.initInterval(),e.instances.push(this)};e.getTime=function(){return+new Date},e.instances=[],e.intervalTimer=null,e.lastInteraction=e.getTime(),e.prototype.destroy=function(n){e.instances.splice(e.instances.indexOf(this),1),0==e.instances.length&&(clearInterval(e.intervalTimer),e.intervalTimer=null)},e.prototype.setIdle=function(){this.isIdle=!0,this.onIdle()},e.setIdle=function(){e.forEach(function(e){e.setIdle()})},e.reset=function(){e.lastInteraction=e.getTime(),e.forEach(function(e){e.isIdle&&(e.isIdle=!1,e.onInteraction())})},e.handleTabChange=function(n){n?e.setIdle():e.reset()},e.forEach=function(n){for(var i=0;i<e.instances.length;i++)try{n(e.instances[i])}catch(t){console.error(t)}},e.update=function(){var n=e.getTime();e.forEach(function(i){i.isIdle||i.idleTime+e.lastInteraction<n&&i.setIdle()})},e.initInterval=function(){null===e.intervalTimer&&(e.intervalTimer=setInterval(e.update,1e3))},document.addEventListener(function(){var e={webkitHidden:"webkitvisibilitychange",msHidden:"msvisibilitychange",mozHidden:"mozvisibilitychange"};for(var n in e)if(n in document)return e[n];return"visibilitychange"}(),function(n){var i=n.type.replace("visibilitychange","");e.handleTabChange(document[""!=i?i+"Hidden":"hidden"])}),window.onmousemove=e.reset,window.onmousedown=e.reset,window.onclick=e.reset,window.onscroll=e.reset,window.onkeypress=e.reset,window.onkeyup=e.reset,window.Idler=e}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment