...

View Full Version : reseting a timer



pthompson2002
08-08-2002, 10:40 AM
thanks for all the help so far!!

My page is split into three frames a header a menu and a main.
I have a countdown timer in the header which, when reaching zero times the user out.
I want to be able to reset the timer, ie when ever the user clicks a link. therefore it only times out if the page is inactive for say 20 minutes.
I have used the reload function on all my menu links, but it is not a suitable approach as there are so many links etc. All the script for the timeout is in the head of the header script.
Is there another way, apart from using reload() to get the timer to "start again" when a link or action is carried out by the user?

thanks

Pete

lifer
08-08-2002, 11:21 AM
It's not totally robust (i.e. it happens whenever the user clicks on the page, not just links), but you can use

<SCRIPT language=Javascript><!--

function whenclicked(){
//put your code in here, e.g. reload()

return true; //this means that the link clicked on will be followed
}

document.captureevent(Event.CLICK);

document.onclick = whenclicked;

// --></script>

I know that's not what you were asking for exactly, but maybe that'll help a bit. I don't know much about timers, I'm afraid.... what object do you use to do the timing??

lifer
08-08-2002, 11:22 AM
oh, erm by the way, you may need to change the "document." to "framename."; you probably figured that out yourself...

pthompson2002
08-08-2002, 11:24 AM
the following is the scirpt I wrote for the time out, this is in the <head>:

var running = false
var endTime = null
var timerID = null
browser = navigator.appName

function startTimer() {
running = true
now = new Date()
now = now.getTime()
// If middle integer set to 60, then just change far right integer to number of mins required
endTime = now + (1000 * 60 * 40)
showCountDown()
}

function showCountDown() {
var now = new Date()
now = now.getTime()
if (endTime - now <= 0) {
stopTimer()
parent.maintask.location.href ='timeOut.htm';
} else {
var delta = new Date(endTime - now)
var theMin = delta.getMinutes()
var theSec = delta.getSeconds()
var theTime = theMin
theTime += ((theSec < 10) ? ":0" : ":") + theSec
if (document.layers)
{
document.ns4timer.document.ns4timer2.document.write(theTime);
document.ns4timer.document.ns4timer2.document.close();
}
if (document.all)
{
timer.innerHTML = '<font size=2>' + theTime + '</font>';
}
if (document.getElementById && document.createRange)
{
rng = document.createRange();
el = document.getElementById("N6timer");
rng.setStartBefore(el);
htmlFrag = rng.createContextualFragment(theTime);
while (el.hasChildNodes())
el.removeChild(el.lastChild);
el.appendChild(htmlFrag);
}
if (running) {
timerID = setTimeout("showCountDown()",1000)
}
}
}


function stopTimer() {
clearTimeout(timerID)
running = false
if (document.layers)
{
document.ns4timer.document.ns4timer2.document.write("0:00");
document.ns4timer.document.ns4timer2.document.close();
}
if (document.all)
{
timer.innerHTML = '<font size=2>0:00</font>';
}
if (document.getElementById && document.createRange)
{
rng = document.createRange();
el = document.getElementById("N6timer");
rng.setStartBefore(el);
htmlFrag = rng.createContextualFragment("0:00");
while (el.hasChildNodes())
el.removeChild(el.lastChild);
el.appendChild(htmlFrag);
}
}

to start the timer an onLoad=startTimer() is placed in the body tag of the header.

lifer
08-08-2002, 11:35 AM
so, you're basically needing to change the variable endTime?

You could use the code I've given you, and place it in the header. I think it should work. Set StartTimer as the function to run onClick, and put "return true;" at the end of StartTimer.

Does it work?

pthompson2002
08-08-2002, 11:36 AM
I'm just implementing it at the mo, I'll let you know.

Cheers for the help

pthompson2002
08-08-2002, 11:44 AM
I couldn't seem to get it to work. I'm not sure if I implemented it correctly - I have only been doing javascript for a week.

lifer
08-08-2002, 11:54 AM
OK, should look like:


function startTimer() {
running = true
now = new Date()
now = now.getTime()
// If middle integer set to 60, then just change far right integer to number of mins required
endTime = now + (1000 * 60 * 40)
showCountDown()

return true;
}

windows.frames('<menuframename>').captureevent(Event.CLICK);
windows.frames('<menuframename>').onclick = startTimer;


If that doesn't work, try putting the last two lines in the menu frame's head (in script tags, of course!), putting "document" instead of "windows.frames('<menuframename>')". If that doesn't work immediately, you could try putting "windows.frames('<headerframename>')." before startTimer. After that, I'm beginning to run out of ideas... :)

pthompson2002
08-08-2002, 12:26 PM
no joy I'm afraid. Thanks for your help anyway. Much appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum