Fun Fun Fun… yet another utility class for your guys’ using pleasure.
Saved as, IdleUserWatcher.as
class IdleUserWatcher
{
// is the user active?
private var __isActive : Boolean = false;
// the id for the interval
private var intervalID : Number;
// how long to wait before calling is idle... default is 10 seconds
private var idleTime : Number = 1000;
// a list of all objects listening
private var listeners : Array;
// readonly property for isActive
public function get isActive () : Boolean
{
return __isActive;
}
public function IdleUserWatcher ( idleTime:Number )
{
if (idleTime != undefined)
{
this.idleTime = idleTime;
}
Mouse.addListener (this);
Key.addListener (this);
listeners = new Array();
}
/* Adds a listener to the listeners list */
public function addListener(listener:Object) : Boolean
{
for (var i:Number=0; i<listeners.length; i++)
{
if (listeners* == listener) return false;
}
listeners.push(listener);
return true;
}
/* Removes a listener from the listener list */
public function removeListener(listener:Object) : Boolean
{
for (var i:Number=0; i<listeners.length; i++)
{
if (listeners* == listener)
{
listeners.splice(i, 1);
return true;
}
}
return false;
}
/* Events */
private function onKeyDown () : Void
{
setIdleInterval();
}
private function onMouseMove () : Void
{
setIdleInterval();
}
/* Private Methods */
private function setIdleInterval()
{
this.__isActive = true;
clearInterval (this.intervalID);
this.intervalID = setInterval (this, "broadcastIdle", this.idleTime, this);
}
private function broadcastIdle(watcher:IdleUserWatcher) :Void
{
watcher.__isActive = false;
for (var i:Number=0; i<listeners.length; i++)
{
listeners*.onUserIdle();
}
clearInterval(watcher.intervalID);
}
}
This an EXTREMELY simple class to use, here is what the code looks like to use it in an fla.
// here you simply tell the object how long to wait for the user
// before deciding he/she is inactive
var idleWatcher = new IdleUserWatcher(1000);
// then we register an object to listen for the onUserIdle() event from
// our IdleUserWatcher object
idleWatcher.addListener(_root);
// IdleUserWatcher has one property, it returns whether or not the user is active
trace(idleWatcher.isActive);
function onUserIdle()
{
trace("BUZZ!!! COME BACK!!");
}
Have fun with this… I’ve officially decided that every site I do will now have a screensaver! WOOT!
_Michael
hey that code was cool. I’m actually going to use it for my next site. I’ll send you a link. thanks. I just wanted to ask if you could help my modification of two tutorials on this site. the XMl slideshow and the Xml photogallery code. located here:
I added a stop and start slideshow function and the previous and next buttons to manually veiw images. The next button however just restarts the slideshow. I was wondering if you can show me the problem. here is the file and the code. Thanks for any help.
:cap: This kind of classes can be used for making applications which requires user presence continuously (or which has a security threat).
Take for example yahoo.
If you have opened up a yahoo mail account and don’t do anything for say 10 minutes it’s session will expire (u need to relogin). This is to keep off the ill usage of your account by some one else.
If the same yahoo thingy is to be done in flash, this class will be very useful.