Click to See Complete Forum and Search --> : Close Window Timer


CaryRW
01-14-2003, 12:17 PM
I am working on adjusting a timer for closing browser windows. The timer works, however, when I change the variable it doesn't seem to really modify the time much. For example, if the variable is set to 5, it close in 12-15 seconds. But if I set to 1, it closes in about 9-11 seconds. I really would like to have it close in about 4-5 seconds. I did try a zero (0) and of course it closed immediately....

Help!!!! Here is the code that I am using....

<script>
function startTime(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
closeTime=hours*3600+mins*60+secs;
closeTime+=1; // Window Timer Variable
Timer();
}

function Timer(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
curTime=hours*3600+mins*60+secs
if (curTime>=closeTime){
self.close();}
else{
window.setTimeout("Timer()",10000)}
}
</script>

I also have an onLoad="startTimer();" command in the body. Any advide as to how to get the timer to closer match the variable would be GREAT!!!

Thanks!


Cary

gil davis
01-14-2003, 12:35 PM
Your code is very complex compared to your description. You also have an apparent typo in the first function header:function startTime(){ because you say you call it usingonLoad="startTimer();"in the body tag.

Assuming the functions are really called correctly, "closeTime" would contain the start time plus 1 millisecond.
closeTime=hours*3600+mins*60+secs;
closeTime+=1;That is not much granularity. It would seem that curTime would always be >= closeTime, and cause the window to close immediately rather than closing after the setTimeout function.

You have not indicated exactly what variable you think you are changing. I would not be surprised that you have not posted something correctly (this is the biggest reason for asking people to post a link rather than posting parts of pages). If you cannot post a link, zip the actual file and attach it to your next post.

As an alternative, just pitch the whole thing and use this:

<script ...>
var numSecs = 5; // adjust for desired time
var howLong = numSecs * 1000; // # of mSecs
function closeWin() {
setTimeout("self.close()", howLong);
}
</script>
...
<body onload="closeWin()">
...

CaryRW
01-14-2003, 12:37 PM
Sounds like a good idea. One problem for me - I am a total newbie at this and really have never coded a single line of JS or VB. I am learning this on the fly. Is there a simple way to modify what I have to incorporate the setTimeout variable? I am guessing that the code that I have could be greatly reduced. The examples on the site you made reference too aren't close enough to what I am doing that I can draw the parallel.

Thanks for the response and any help your willing to offer!


Cary

CaryRW
01-14-2003, 10:19 PM
Gil,

The code works great! Thanks a million!!!! I knew there had to be something simple....:D


Cary