View Full Version : Detecting user inactivity..
MichaelxxOA
March 8th, 2006, 02:10 AM
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[i] == 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[i] == 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[i].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
phorte
March 8th, 2006, 02:46 AM
Wow man, another class from you.... You just keep comin out with them dont ya. haha. Nah tis awsome work. Very useful stuff actually. Nice.
MichaelxxOA
March 8th, 2006, 02:49 AM
I should probably not be giving everyone this stuff.. but whatever. You have any ideas?? I need them. To be perfectly honest I'm pissed off at this girl.. so I am taking all this frustration and anger and putting it in to programming.. that's where all of this stuff is coming from so fast ;). Take Care. and enjoy, please
_Michael
phorte
March 8th, 2006, 03:52 AM
I should probably not be giving everyone this stuff.. but whatever. You have any ideas?? I need them. To be perfectly honest I'm pissed off at this girl.. so I am taking all this frustration and anger and putting it in to programming.. that's where all of this stuff is coming from so fast ;). Take Care. and enjoy, please
_Michael
HAHA :lol: notin like a good bit of women troubles to make u wanna.... umm program.. hmm, interesting.. :D
Pasquale
March 8th, 2006, 04:24 AM
so - say if i cause you to have a woman problem could you code me a program?
hahahah :P
MichaelxxOA
March 8th, 2006, 05:54 AM
absolutely, you just have to make the check out to Michael Avila when I'm done... mua ha ha. Lol, take care.
_michael
LunaSolar
March 8th, 2006, 09:25 AM
This is a real fun one. I never thought of that, but what a good idea.
~SomeOne~
March 8th, 2006, 10:43 AM
hhhhhhh man that is great idea ..
i realy never thought of some thing like that .too
it will be good for site who loves there vistros :D
MichaelxxOA
March 8th, 2006, 10:54 AM
Lol thanks guys, most of what I do is more of software development (if you haven't noticed) rather than website design, although I do ALOT of that. So you can see where these little things come from. Take Care. If you guys need anything like this or these posts please ask, I need stuff to do. Bye!
_Michael
Oli-G
March 8th, 2006, 05:51 PM
Awesome work, thanks a lot.
lunatic
March 8th, 2006, 05:54 PM
Don't listen to aussie devil - I think its awesome you are posting this stuff for everyone to use! :thumb:
Greenwidth
March 8th, 2006, 05:55 PM
Nice idea! :thumb2:
MichaelxxOA
March 8th, 2006, 08:08 PM
Thanks thanks guys, anyone use this yet? I'm going to make a screensaver and then I'll post the example. Take Care.
_michael
gonzales
March 10th, 2006, 01:21 PM
how do you implement this class in flash? ive never used external classes before...
MichaelxxOA
March 10th, 2006, 01:42 PM
Have you used custom classes before??
_Michael
gonzales
March 10th, 2006, 02:04 PM
no, thats why im asking. do you include it somehow like #include or something?
EDIT:
okay, i stuck it in my classes folder and it seems to work fine!!
i assume i am using it correctly?
MichaelxxOA
March 10th, 2006, 05:44 PM
Yes sir :) If you need help with anything please ask again. Take care, and thanks for the feedback.
_Michael
phorte
March 10th, 2006, 07:02 PM
This is bit off topic, but class related. You know what would be really useful, extra functions for the date constructor. I was building somthing yesterday, and i wanted to get the day of the week of this certain date object, then i wanted to get the prefix for the date (Eg, st, nd, rd, th), then i also wanted to get the current month. Alrite it wasnt that much of a hassle, but it still was a hassle. Would be nice to just be able to go: myDate.getWeekDay(), and it will return you with Saturday. Or say, myDate.getDatePrefix() and it will return you with st, or nd etc.
Anyway, just an idea. Prob wouldnt be too hard too build only took me about an extra 10lines or somtin in my script. I would try and build it myself, but still learning the ways of these class functions, well actually im still stuck back in AS1.0, havent learned all these new fangled scripts. :elderly:
anyway, yeah, just a proposition.
-Aussie Devil
MichaelxxOA
March 10th, 2006, 07:34 PM
Yeah I'll put something together, I'm not to fond of the Data class as it is.. I might end up writing a DateTime class here pretty soon. I'll post a new thread when I have something. Take Care.
_michael
bear
March 10th, 2006, 11:37 PM
snip
-Aussie Devil
...try creating a class that extends/uses the Date Class. Day itself gives out the day of the week in a numeric value, and date gives out the day according to the month. Utilizing an array for the date and time can be of help.
I would also like to attempt this...if Michael would let me =)
Impressive show, btw.
MichaelxxOA
March 11th, 2006, 12:35 AM
I am not in control of what you can or can't do. Do it, that's what your supposed to as a programmer ;).
I am still going to work on mine though, we can compare and see what methods are and arent' necessary. You've got the right idea though, utlizize what the Date class gives by extending it and just adding the get/format methods. It really shouldn't be that big of a deal. Take Care.
_Michale
Smee
March 11th, 2006, 07:03 PM
Oh boy, website screensavers :fight:
I just hope they don't pop up when I'm sitting there reading something on the website and not interacting.
rille31
March 12th, 2006, 05:41 AM
Can anybody post an example?
Cool idea btw!
indian_blues
March 15th, 2006, 08:03 AM
:bounce:
kernel_flash
March 17th, 2006, 12:58 AM
Thanks a lot MicahaelxxOA,
This has helped me save lot of time.
Thanks again.
MichaelxxOA
March 17th, 2006, 01:39 AM
No problem, I'm glad I was able to help. Take Care. And when you're done can we see what you made with it :).
_Michael
kernel_flash
March 17th, 2006, 01:45 AM
Actually I'm making a work-flow editor which is web based.
It is a single sign-on application. So when the user is inactive for certain amount of time, the application should time out and close saving the work.
rille31
March 17th, 2006, 07:13 AM
this sounds cool, examples anyone?
TheCanadian
March 17th, 2006, 02:11 PM
this sounds cool, examples anyone?
There's one in the first post ;)
Lindquist
March 17th, 2006, 02:13 PM
If your taking suggestions for classes, here's an idea. How about writing a "Reflection" class that could take all the mcs on the stage and create reflections (with proportionality, perspective, fading, etc.) on an imaginary floor. Sounds like fun.
MichaelxxOA
March 17th, 2006, 02:20 PM
What about an mc transform class, that you can pass an instance of a movieclip into the constructor, then call it's reflect() method... passing in the the position on the Y axis at which the floor is?
_Michael
Lindquist
March 17th, 2006, 03:58 PM
In Flash 8, mcs already have a transform property which accepts Matrices, so that might get kind of confusing, but I like where you're going with this.
What would be nice is a Distort class that would allow a Matrix or Object to accept _x and _y values for each corner of the mc. I know Flash 8 already has flash.geom.Matrix, but that only allows for skewing as far as I know.
Jolly_Fat_Man
March 24th, 2006, 02:52 PM
Great work!
Thanks for sharing!
MichaelxxOA
March 24th, 2006, 09:23 PM
No worries, post your use of it if you'd like, I"m sure we will.
_Michael
JoshuaJonah
March 27th, 2006, 01:43 PM
lol... make a new thread.
info@rkirby.com
March 31st, 2006, 02:07 PM
Has anyone got a small FLA they can post that has an example of this class... to help the uneducated (such as myself)
kernel_flash
April 3rd, 2006, 01:08 AM
: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.
So now Michael do you feel like calling yahoo???
MichaelxxOA
April 3rd, 2006, 03:25 AM
big checks??
Honestly I'm working on a new version, I'm going to benchmarking them and see how well they run.. This one has it's drawbacks.
_Michael
pips
April 25th, 2006, 05:53 PM
I have made this already to create some sort of screensaver inside Flash movie ;)
beerad@mac.com
September 18th, 2006, 12:49 AM
Fun Fun Fun... yet another utility class for your guys' using pleasure.
Saved as, IdleUserWatcher.as
// how long to wait before calling is idle... default is 10 seconds
private var idleTime : Number = 1000;
// here you simply tell the object how long to wait for the user
// before deciding he/she is inactive
var idleWatcher = new IdleUserWatcher(1000);
Great script. I am having an issue with changing the time. I am adjusting the two lines above but it always seems to go to idle mode after the 10 seconds.
Any help would be great.
waywoodr
September 19th, 2006, 09:57 AM
If your taking suggestions for classes, here's an idea. How about writing a "Reflection" class that could take all the mcs on the stage and create reflections (with proportionality, perspective, fading, etc.) on an imaginary floor. Sounds like fun.
Thats real easy - all you need to do is click on the following link [ http://www.gotoandlearn.com/forum/viewtopic.php?t=770 ] Its that simple :stunned:
ZeypheR
September 20th, 2006, 04:04 AM
I've never dealt with external classes before and I'm wondering if someone could quickly tell me how to prepare them for use?
Please and thanks!
LittleFenris
September 24th, 2006, 10:33 AM
No problem, I'm glad I was able to help. Take Care. And when you're done can we see what you made with it :).
_Michael
Awesome little class you made! This just saved me hours of trying to figure out setInterval or some other way to make my character change frames when the user hasn't done anything for a while (your basic "sonic taps his feet when he's standing still too long addition to the site). Once the site is done I'll post up a link. Right now its not too exciting, but your code just saved me loads of time.
Thanks a bunch.
evildrummer
September 26th, 2006, 10:04 AM
Damn that would be so usefull in games and such so it can detect if you have buggered off and started watching TV so the enemies dont kill your char, even though they should seeming though you forgot to pause.
tedc
October 3rd, 2006, 07:00 PM
Great!! Why hasnt anyone thought of that before? I think it can be best used for online applications such as flash blogs or the sort, for automatic logout or autosave (wow, how cool is that?)
I think I will use it on a site I am doing now for the logo who will fall asleep if you're gone. Im not so familiar with external classes but I'd like to know if it's possible to detect when the user returns from an unidle state?
bcswebstudio
November 21st, 2006, 11:24 AM
I am anxious to use this class in a neat kiosk interface I'm working on: inactivity would set up the kiosk for the next user.
However, I'm having trouble importing the class. I've put your orig IdleUserWatcher.as in the same directory as my fla/swf, and I'm using "import IdleUserWatcher;" in the top of the frame instanciating the class. The error I get is:
**Error** Scene=Scene 1, layer=ui, frame=17:Line 11: The class or interface 'IdleUserWatcher' could not be loaded.
var idleWatcher = new IdleUserWatcher(3000);
I think the only deviation from your example was changing 1000 to 3000 ms... please help?
bcswebstudio
November 21st, 2006, 11:53 AM
ok, genius chiming in again here..
I was so flustered with my inability to get this to work in several config's that when I finally had the combination that worked, I was working with the wrong folder/path. Ooooops. Sorry for cluttering the thread :) Looking forward to poking around this class to see if it will work for me.
bcswebstudio
November 21st, 2006, 04:28 PM
Cool. Thanks lots for this simple timeout/user inactivity detector. I am using it as hoped in a kiosk-- if a user doesn't interact for x-seconds, the kiosk goes back to the initial/start screen. Perfecto! :D
I would post a sample, but it's a .exe project created by MDM Zinc so my Flash could talk to an Access database.
scoult01
November 22nd, 2006, 09:02 PM
Great class dude, thanks for this.
MichaelxxOA
December 14th, 2006, 06:08 PM
Hey guys sorry I haven't been on here in FOREVER!!!!
Thanks guys, I am working on some other stuff... I'll post about it here within the next month or too.
It seems like every person that asked a question figured it out in some way or another. If not go ahead and ask, and I'll try to be around here more.
Take care.
Michael
suicidePills
December 17th, 2006, 05:22 PM
So, why not put the interval object in the class itself and just set the wait time indirectly? If you need multiple timers, you can just created multiple instances of the class...right?
-sp
bcswebstudio
February 16th, 2007, 12:57 AM
Great class. I am using it in a self-running kiosk to detect user abandonment.
Is it possible to have two IdleUserWatcher's on the same timeline? And what about within different movie clips? They would call the same onUserIdle function...
thanks
rille31
February 17th, 2007, 09:21 AM
Im trying to make a screensvar for a website
so in the onUserIdle function I put
function onUserIdle() {
sleeper._visible = true;
}
to make the scrensaver visible
but what function should I call to hide it if any?
MichaelxxOA
February 17th, 2007, 09:29 AM
function onUserIdle()
{
sleeper._visible = true;
onMouseMove = onUserActivate;
}
function onUserActivate()
{
sleeper._visible = false;
onMouseMove = null;
}
That should work *shrugs*
Michael
bcswebstudio
February 18th, 2007, 09:53 AM
Ah, great idea!
Michael- that would be a logical extension of the IdleUserWatcher class, wouldn't it? It's simple without it, as you demonstrated, but it seems to be part of the same package..
indigo.child
February 19th, 2007, 05:39 PM
I should probably not be giving everyone this stuff.. but whatever. You have any ideas?? I need them. To be perfectly honest I'm pissed off at this girl.. so I am taking all this frustration and anger and putting it in to programming.. that's where all of this stuff is coming from so fast ;). Take Care. and enjoy, please
_Michael
I think we should "send" you more girls to piss you off, regularly. ;)
rille31
February 22nd, 2007, 05:51 PM
Here is a live example with a simple fade in effect
www.rille.net (http://www.rille.net)
function onUserIdle():Void {
sleeper._alpha = 0;
sleeper._visible = true;
var fadeIn:Tween = new Tween(sleeper, "_alpha", Strong.easeOut, 0, 90, 1, true);
onMouseMove = onUserActivate;
Key.addListener(myListener);
}
function onUserActivate():Void {
Key.removeListener(myListener);
var fadeOut:Tween = new Tween(sleeper, "_alpha", Strong.easeOut, 90, 0, 1, true);
onMouseMove = null;
fadeOut.onMotionFinished = function():Void {
sleeper._visible = false;
sleeper._alpha = 100;
};
}
var myListener:Object = new Object();
myListener.onKeyDown = onUserActivate;
bcswebstudio
February 23rd, 2007, 10:38 AM
nice one..
and Michael, I should report that my kiosk interface is working nicely with two times-out: one is just for a particular interface-- a user types a name to search for with onscreen keypad, finds the name, and if he stares at it long enough, the search screen itself reinitializes. The other is an overall inactivity meter indicative of "user abandoned the kiosk." This one detects inactivity, unloads whatever current movieclip is loaded, and starts over from frame 1 of _root again. works like a charm! :D
MichaelxxOA
February 23rd, 2007, 11:39 AM
Hey BCWeb and Rille both of those sound great. You guys are too awesome, honestly.
BCWeb, that does seem like a logical extension, I will most likely be reworking this for actionscript 3 if you guys are interested.
Again, awesome work guys. I appreciate you posting back here.
-Michael
BetaWar
March 15th, 2007, 09:05 PM
God, okay I am sorry, and feel really dumb right now, but how do you access an external as file?
shortly
April 8th, 2007, 08:11 AM
God, okay I am sorry, and feel really dumb right now, but how do you access an external as file?
hi betawar... from my reading, just make sure that the .as file is in the same directory as the .FLA and all should be fine. when you compile the swf the .as code is included in it - if it's being called. i could be completely wrong about that of course!
i'm trying to get this to work myself and am a bit stuck. i'm not getting a loading error, but using the code at the start of the thread when try and trace(idleWatcher.inActive) i simply get undefined in the output box. anyone got any idea why that is?
i haven't really used classes or listners that much so i'm reading up on it all. this class looks like it will help me immensely. i think i need a simple example - perhaps a downloadable 1 frame FLA. anyone?
ta :puzzle:
Hunson
April 8th, 2007, 11:51 PM
thank you man!
bcswebstudio
October 15th, 2007, 04:17 PM
Has anyone had any bad luck implementing this timeout timer script which causes the cpu cycles on his app to freeze up entirely? My self-running kiosk, when left unattended (or maybe some users have interacted, I don't know) for several weeks slows to a crawl.
I assume it's my own implementation of the posted script and perhaps due to my use of two at a time, but I haven't had time to dig in yet. I thought I'd put out feelers in the meantime to learn others' experiences. When I find the issue, I'll be sure to respond here again. It's a great script and a good idea.
thanks
Multi-Task
November 19th, 2007, 12:37 PM
HHAHAHA all I have to say is thank you. I was just posting about this in the forums.
bcswebstudio
November 20th, 2007, 10:09 AM
Multi-Task- are you by chance referring to the 100% CPU ramp up/system crawl described in my post? I really am at a loss on this one as I've not had a chance to sit down and figure it out.
I have seen other actionscript-centry swf's behave similarly as well as others run smoothly-- there must be some simple solution...
sania
December 16th, 2008, 03:51 PM
Michael,
If you still login, would you pls. assist. I am fairly new to AS. Is the function "onUserIdle()' around ln. 81 the function to be called in idle mode? I will greatly appreciate if you could assist with the way the class and its functions work.
Thanks always,
Sania
Fun Fun Fun... yet another utility class for your guys' using pleasure.
Saved as, IdleUserWatcher.as
ActionScript Code:
</p>
<p>class IdleUserWatcher</p>
<p>{</p>
<p> // is the user active?</p>
<p> private var __isActive : Boolean = false;</p>
<p> // the id for the interval</p>
<p> private var intervalID : Number;</p>
<p> // how long to wait before calling is idle... default is 10 seconds</p>
<p> private var idleTime : Number = 1000;</p>
<p> // a list of all objects listening</p>
<p> private var listeners : Array;</p>
<p> // readonly property for isActive</p>
<p> public function get isActive () : Boolean</p>
<p> {</p>
<p> return __isActive;</p>
<p> }</p>
<p> </p>
<p> public function IdleUserWatcher ( idleTime:Number )</p>
<p> {</p>
<p> if (idleTime != undefined)</p>
<p> {</p>
<p> this.idleTime = idleTime;</p>
<p> }</p>
<p> </p>
<p> Mouse.addListener (this);</p>
<p> Key.addListener (this);</p>
<p> listeners = new Array();</p>
<p> </p>
<p> }</p>
<p> /* Adds a listener to the listeners list */</p>
<p> public function addListener(listener:Object) : Boolean</p>
<p> {</p>
<p> for (var i:Number=0; i<listeners.length; i++)</p>
<p> {</p>
<p> if (listeners == listener) return false;</p>
<p> }</p>
<p> listeners.push(listener);</p>
<p> return true;</p>
<p> }</p>
<p> [I]/* Removes a listener from the listener list */</p>
<p> public function removeListener(listener:Object) : Boolean</p>
<p> {</p>
<p> for (var i:Number=0; i<listeners.length; i++)</p>
<p> {</p>
<p> if (listeners == listener)</p>
<p> {</p>
<p> listeners.splice(i, 1);</p>
<p> return true;</p>
<p> }</p>
<p> }</p>
<p> return false;</p>
<p> }</p>
<p> </p>
<p> [I]/* Events */</p>
<p> private function onKeyDown () : Void</p>
<p> {</p>
<p> setIdleInterval();</p>
<p> }</p>
<p> private function onMouseMove () : Void</p>
<p> {</p>
<p> setIdleInterval();</p>
<p> }</p>
<p> </p>
<p> /* Private Methods */</p>
<p> private function setIdleInterval()</p>
<p> {</p>
<p> this.__isActive = true;</p>
<p> clearInterval (this.intervalID);</p>
<p> this.intervalID = setInterval (this, "broadcastIdle", this.idleTime, this);</p>
<p> }</p>
<p> private function broadcastIdle(watcher:IdleUserWatcher) :Void</p>
<p> {</p>
<p> watcher.__isActive = false;</p>
<p> for (var i:Number=0; i<listeners.length; i++)</p>
<p> {</p>
<p> listeners.onUserIdle();</p>
<p> }</p>
<p> clearInterval(watcher.intervalID);</p>
<p> }</p>
<p>}</p>
<p>
This an EXTREMELY simple class to use, here is what the code looks like to use it in an fla.
ActionScript Code:
</p>
<p>[I]// here you simply tell the object how long to wait for the user</p>
<p>// before deciding he/she is inactive</p>
<p>var idleWatcher = new IdleUserWatcher(1000);</p>
<p> </p>
<p>// then we register an object to listen for the onUserIdle() event from</p>
<p>// our IdleUserWatcher object</p>
<p>idleWatcher.addListener(_root);</p>
<p> </p>
<p>// IdleUserWatcher has one property, it returns whether or not the user is active</p>
<p>trace(idleWatcher.isActive);</p>
<p> </p>
<p>function onUserIdle()</p>
<p>{</p>
<p> trace("BUZZ!!! COME BACK!!");</p>
<p>}</p>
<p>
Have fun with this.. I've officially decided that every site I do will now have a screensaver! WOOT!
_Michael
birdwing
December 17th, 2008, 01:44 AM
bcswebstudio: It sounds to me like the movie clips are just being hidden, but not removed from memory.
I would try instead of loading movie clips loading external swf's instead.
If you close an externally loaded SWF it is removed from memory, sometimes (with flashes garbage handling issues) Movie clips aren't removed from memory.
this is all speculation since I don't know exactly how you coded or worked everything.
rille31
January 25th, 2010, 06:02 PM
bumping up the old thread
are there any as3 examples?
thanks, love it btw
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.