A reverse-chronological history, accessible as an array at log.history
I have also included (I removed it) a shorthand logargs() function that is useful when you’re inside a function and want to know the context and arguments passed in. I use it a lot in ajax callbacks. Worth noting that it uses arguments.callee.caller, which will be deprecated in ECMAScript 5 Strict mode. :( I killed off logargs cuz nobody lurved it like I did. If you want it… pastie.org/1033665
The code:
123456789
// usage: log('inside coolFunc',this,arguments);// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/window.log=function(){log.history=log.history||[];// store logs to an array for referencelog.history.push(arguments);if(this.console){console.log(Array.prototype.slice.call(arguments));}};
Interestingly, the minified version of this script is smaller (262 148 bytes), and arguably more useful, than the minified firebugx.js, which I’ve covered before. Plus it has one more feature.
Plus Firebug lite?
You got it. This bookmarklet will add firebug lite, and then output the logged history when it’s ready:
After writing this, I worked with Ben Alman on a more comprehensive and robust logging script. It’s excellent if you take full advantage of the console API. And you should be aware that Safari 4 and Chrome 2 have most of that API supported. Make full use of it and don’t you dare type another alert()!
2010.05.20: Updated log code. Fixed some edge case bugs.
2010.07.06: Updated code again. removed logargs. link to pretty viewings in the gist
Considered mousewheel as activity, in addition to keyboard and mouse movement.
Gave it a bit more jQuery-ish of an API
To use:
12345678910111213141516
// idleTimer() takes an optional argument that defines the idle timeout// timeout is in milliseconds; defaults to 30000$.idleTimer(10000);$(document).bind("idle.idleTimer",function(){// function you want to fire when the user goes idle});$(document).bind("active.idleTimer",function(){// function you want to fire when the user becomes active again});// pass the string 'destroy' to stop the timer$.idleTimer('destroy');
Note: If you want to change the timeout interval, you’ll have to destroy the existing timer first.
2009.09.22: I’ve updated the idleTimer script for a few more features…
12345
// you can query if the user is idle or not with data()$.data(document,'idleTimer');// 'idle' or 'active'// you can get time elapsed since user when idle/active$.idleTimer('getElapsedTime');// time since state change in ms
2010.05.11: Due to popular demand, there is now support for multiple timers!!
Why would you want this? Lets say you want ..
One Timer to restore the forms, messages boxes, etc.
Another Timer of a different length timeout to notify about expiration sessions.
123456
// bind to specific elements, allows for multiple timer instances$(elem).idleTimer(30*1000);$(elem).idleTimer('destroy');$(elem).idleTimer('getElapsedTime');// some ms something$.data(elem,'idleTimer');// 'idle' or 'active'
Notice this new API is on $.fn.idleTimer, as in it works on a jQuery collection instead of just the jQuery global object.
If you’re using this along with the old $.idleTimer api, you should not do $(document).idleTimer(...)
All these element-bound timers will only watch for events inside of them. You may just want to watch page-level activity, in which case you may set up your timers on document, document.documentElement, and document.body. Those will allow three separate timers that will catch all page activity.