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!

close after 5 seconds with visible countdown 2

Status
Not open for further replies.

gameon

Programmer
Feb 23, 2001
325
0
0
GB
Hi, does anyone know how to get a window to close after 5 seconds, with a countdown that is visible on the page.

M@)
 
<script language=&quot;javascript&quot;>
setTimeout(&quot;window.close,&quot;,300000)
</script>
 
Hi, why 300000 and if I can have an output of the seconds counting down?

Cheers for your help...

M@)

 
Heres one I made earlier:

<script>
var countdownFrom = 5; // number of seconds
var countdownwin;

function CountDownPopup() {
countdownwin = window.open(&quot;about:blank&quot;,&quot;&quot;,&quot;height=200,width=200&quot;);
self.setInterval('CountDownTimer()',1000)
}
function CountDownTimer()
{
if (countdownFrom==0) {countdownwin.close(); window.close(); }
else {
doc = countdownwin.document;
doc.open('text/html');
doc.write(&quot;Closing in &quot;+countdownFrom+&quot; seconds&quot;);
doc.close();
}
countdownFrom--
}
</script>


just call CountDownPopup() to start the countdown. This script doesnt check to see if the popup contdown window has been closed during the countdown, this is something you may wanna add to the code.
 
I really want to not have a popup and keep it on the same page. Can I just delete the popup bit?
 
I tried this, but broke it...

var countdownFrom = 5; // number of seconds
var countdownwin;



self.setInterval('CountDownTimer()',1000)


function CountDownTimer()
{
if (countdownFrom==0) {countdownwin.close(); window.close(); }
else {
document.write(&quot;Closing in &quot;+countdownFrom+&quot; seconds&quot;);
doc.close();
}
countdownFrom--

}
 
Yeah you could replace it with an area on the page in a Div Tag Span Tag or anything with an ID (Layer for netscape4)

somewhere in your HTML

<Div id=&quot;CountDownTime&quot;></div>

replace CountDownPopup() with:

var stp;
function CountDownStart() {
stp = setInterval(&quot;CountDownTimer('CountDownTime')&quot;,1000)
}

then replace the CountdownTimer() with

function CountDownTimer(id)
{
if (countdownFrom==0) {clearInterval(stp); window.close(); }
else {
var x
var cntText = &quot;Closing in &quot;+countdownFrom+&quot; seconds&quot;;

if (document.getElementById)
{
x = document.getElementById(id);
x.innerHTML = cntText; }
else if (document.all)
{
x = document.all[id];
x.innerHTML = cntText; }
}
countdownFrom--
}


This code will work for IE and N6 but not N4.x, if you are supporting Netscrap4 good luck mate!
 
Sorry, you must think me an f-wit. But:



The following doesn't work:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>

<script>
<Div id=&quot;CountDownTime&quot;></div>

var countdownFrom = 5; // number of seconds
var countdownwin;

var stp;
function CountDownStart() {
stp = setInterval(&quot;CountDownTimer('CountDownTime')&quot;,1000)
}

function CountDownTimer(id)
{
if (countdownFrom==0) {clearInterval(stp); window.close(); }
else {
var x
var cntText = &quot;Closing in &quot;+countdownFrom+&quot; seconds&quot;;

if (document.getElementById)
{
x = document.getElementById(id);
x.innerHTML = cntText; }
else if (document.all)
{
x = document.all[id];
x.innerHTML = cntText; }
}
countdownFrom--
}

</script>
<title>Untitled</title>
</head>

<body>




</body>
</html>
 
Yes I do! :)

Only joking, simple mistake, put the div tag in between the Body tags (which is where all your actual content HTML is meant to go) and call the CountDownStart() function in the body onload, so just cut n paste the following:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>

<script>

var countdownFrom = 5; // number of seconds
var countdownwin;

var stp;
function CountDownStart() {
stp = setInterval(&quot;CountDownTimer('CountDownTime')&quot;,1000)
}

function CountDownTimer(id)
{
if (countdownFrom==0) {clearInterval(stp); window.close(); }
else {
var x
var cntText = &quot;Closing in &quot;+countdownFrom+&quot; seconds&quot;;

if (document.getElementById)
{
x = document.getElementById(id);
x.innerHTML = cntText; }
else if (document.all)
{
x = document.all[id];
x.innerHTML = cntText; }
}
countdownFrom--
}

</script>
<title>Untitled</title>
</head>

<body onload=&quot;CountDownStart()&quot;>

<Div id=&quot;CountDownTime&quot;></div>



</body>
</html>
 
Thank you so much...

If you ever want some nice flash work done - let me know...

M@
 
Sorry so late on this one, but here's a cross-browser solution that should work in IE, NS4.x & NS6+:
[tt]
<html>
<head>
<title>Countdown Test</title>
<script type=&quot;text/javascript&quot;>
<!--
var countdownFrom=5; // number of seconds
var countdownwin;
var stp;

function CountDownStart(){
stp=setInterval(&quot;CountDownTimer('CountDownTime')&quot;,1000)
}

function CountDownTimer(id){
if(countdownFrom==0){
clearInterval(stp);
self.close();
}else{
var x;
var cntText='Closing in '+countdownFrom+' seconds';
if(document.getElementById){
x=document.getElementById(id);
x.innerHTML=cntText;
}else if(document.all){
x=document.all[id];
x.innerHTML=cntText;
}else if(document.layers){
x=document.NNCountDownTime;
x.document.write(cntText);
x.document.close();
}
}
countdownFrom--;
}
//-->
</script>
</head>
<body>
<div id=&quot;CountDownTime&quot;><layer name=&quot;NNCountDownTime&quot;></layer></div>

<script type=&quot;text/javascript&quot;>
<!--
window.onload=CountDownStart;
//-->
</script>
</body>
</html>
[/tt] Paul Ellis
[hammer] I've been programming since you had to punch holes in cards to code. (Always remember, number your cards!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top