Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WANTED - a setTimeOut script please! 2

Status
Not open for further replies.

andycheese

Programmer
Jun 10, 2002
38
0
0
GB
How could I set up a script that runs down a counter, then executes a function? What I want to do is check for user inactivity, and if they have not been active (like many users ;-) ) for a specified time, I want to do something. Similarly, if they initiate an action, I want the counter to be reset. To put it more simply, I want to emulate the basic behaviour of a screen saver, within the browser (IE, if it matters).

Thanks in advance for any tips, pointers, partial or whole solutions! :)
 
jsut a note on the link to FAQ
use the faq216-2700 and not the address in the URL
this causes some other issues when the url is referenced. [smile] _______________________________________________
[sub]{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924

 
Thanks, although it's a bit more complicated than what I had in mind ;-)! I think I'll attempt to write my own, and post my efforts if I can make it work (and even if I can't).
 
<script language=&quot;Javascript&quot;>
// unless you cancel bubble this should work
var objTimer = new Object();
var intTimeout = new Number(10000);

function docActive(){
clearTimeout(intTimeout);
intTimeout = new Number(10000);
}


function checkActivity(){
// no activety for intTimeout milliseconds
}
objTimer = setTimeout('checkActivity();',intTimeout);
</script>

<body onclick=&quot;docActive();&quot; onkeyup=&quot;docActive();&quot; onotherevents=&quot;docActive();&quot;>
</body>

 
euuuuhr, sorry, the script got some errors. Here is a working sample:
<script language=&quot;Javascript&quot;>
// unless you cancel bubble this should work
var objTimer = new Object();
var intTimeout = new Number(2000);

function docActive(){
clearTimeout(objTimer);
objTimer = setTimeout('checkActivity();',intTimeout);

}


function checkActivity(){
// no activety for intTimeout milliseconds
}
objTimer = setTimeout('checkActivity();',intTimeout);
</script>

<body onclick=&quot;docActive();&quot; onkeyup=&quot;docActive();&quot; onotherevents=&quot;docActive();&quot;>
</body>

 
Thank you very much harmmeijer - this is my current work in progress script (not tested, but may well work)

<script language=&quot;Javascript&quot;>
var milliSeconds;
var the_timeout;
function countDown()
{
milliSeconds = document.form1.seconds.value * 1000;
the_timeout = setTimeout(&quot;logOff();&quot;, milliSeconds);
}
function logOff()
{
doSomething();
}
//-->
</script>
</head>
<body onLoad=&quot;countDown();&quot; onMousemove=&quot;clearTimeout(the_timeout); countDown();&quot; onKeypress=&quot;clearTimeout(the_timeout); countDown();&quot;>

What do you reckon?
 
It should work, if any object within the body sets the cancelbubble to true (<input type=button onclick=&quot;cancelBubble =true;...) then the document won't get the click event (if you click on that particular button).

I would change the line:
milliSeconds = parseInt(document.form1.seconds.value) * 1000;

If the value of seconds does contain an Integer, if a user can fill this out then I would make sure it is a number.
 
Thanks once again. I'd forgotten about parseInt - much safer way of doing things.

I've actually decided to use your script as i like it better than mine (i think it's a bit more robust, plus I don't trust my coding ;-)). What's this cancelBubble thing you write of? Have I missed something, or are you using it as a metaphor? Or (more likely) am I being thick?

Cheers!
 
<body onclclick=1>
<div onclick=2>
<button onclick=3>
</div>
</body>

If I click the button then the function 3 will be executed, if I don't cancel bubble the function 2 will be executed,
if I don't cancel bubble the function 1 will be executed.

So without canceling bubble (default) the body allways gets clicked.


Gteetings,

H.M
 
U could redirect the user to another page if there is no activity for x amount of time.
here is a snippet of code I'd use for such a thing :

var timer;

function startOver()
{
location.href = 'startPage.html';
}

function resetTimer()
{
timer = setTimeout(&quot;startOver()&quot;, 600000);
return true;
}

document.onmousemove = resetTimer;
document.onKeyPress = resetTimer;

I hope this helps u...
-Mon3y is the r00t of all evil and every man needs roots-
 
Thanks for the help guys, some good ideas and useful tips here.
harmmeijer - so what you're saying (basically), is that clicks or events always cascade down to the body element, unless something is called in-between-times. That's worth knowing.
 
Ok, I think I need a bit more help.... :-D

The script works quite nicely, up to a point - the timer doesn't appear to be reset on an event, so the page loads, timer counts down, and then it goes away and does something. No amount of clicking, mouse waving (cursing) etc seems to stop it. Would some kind person mind telling me what I've managed to break? (for the purposes of testing, i've set timeFromUser to = 10)

<script language=&quot;Javascript&quot;>
var timeFromUser;
var objTimer;
var intTimeout;
function docActive()
{
timeFromUser = parseInt(window.document.the_form.TIMEOUT.value);
objTimer = new Object();
intTimeout = new Number(timeFromUser * 1000);
clearTimeout(objTimer);
objTimer = setTimeout('checkActivity();',intTimeout);
return true;
}
function checkActivity()
{
this bit redirects the user to another screen.
}
document.onmousemove = docActive;
document.onKeyPress = docActive;
document.onClick = docActive;
</script>
 
Intialize your varaibles outside the docactive function:

<script language=&quot;Javascript&quot;>
var timeFromUser;
var objTimer;
var intTimeout;
objTimer = new Object();
timeFromUser = parseInt(&quot;2&quot;);
intTimeout = new Number(timeFromUser * 1000);
function docActive()
{
clearTimeout(objTimer);
objTimer = setTimeout('checkActivity();',intTimeout);
return true;
}
function checkActivity()
{
alert(&quot;timed out&quot;);
}
document.onmousemove = docActive;
document.onKeyPress = docActive;
document.onClick = docActive;
</script>
 
Thanks, that does work a lot better, BUT if I use the line timeFromUser = parseInt(window.document.the_form.TIMEOUT.value);

Then as the page loads I get a pop-up warning box saying &quot;window.document.the_form.TIMEOUT is null or not an object&quot;. I'm positive it is neither! The form and hidden field (TIMEOUT) are named correctly, and TIMEOUT is holding a value of &quot;0010&quot;.

Grrr. :-(
 
Script outside a function executes as the page loads from top to bottom. The script might be before the textbox (see example below).

<script>
try{
var objTB = document.getElementById('txt');
var nr = parseInt(objTB.value,10);
alert('value of the textbox is: ' + objTB.value);
alert('value of the nr: ' + nr);
}catch(e){
alert(&quot;some error before the textbox: &quot; + e.description);
}
</script>
<input type=text value='10' id='txt'>
<script>
try{
var objTB = document.getElementById('txt');
var nr = parseInt(objTB.value,10);
alert('value of the textbox is: ' + objTB.value);
alert('value of the nr: ' + nr);
}catch(e){
alert(&quot;some error after the textbox: &quot; + e.description);
}
</script>
 
Thanks, that has solved it! You see, the function was declared in the < head > and I was calling it onLoad, which obviously was before the form object was populated.

Thanks very much for your help, it has been much appreciated!
 
Actually in the onload event the form is pupulated.
But since timeFromUser is not in the function it is executed as soon as that line loads.
timeFromUser depends on the textbox to be there.
 
Ahh, I see. Well, you learn something more every day :-D Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top