469,360 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

make a div disappear after a few seconds

Hi all,

I've created a DIV with important notes for my site. I've a Close button on
it, but would like to have a timer that automatically close the div is it
hasn't been closed after a few seconds (let's say 5 seconds). Thanks for
your help.

How to do so ?

Oct 13 '06 #1
5 33414
Bob Bedford wrote:
Hi all,

I've created a DIV with important notes for my site. I've a Close button
on it, but would like to have a timer that automatically close the div is
it hasn't been closed after a few seconds (let's say 5 seconds). Thanks
for your help.

How to do so ?
Hi

Use setTimeout();

eg:
<div id="helpdiv" style="display:block">
helpstuff here
</div>

<script type="text/javascript">
// close the div in 5 secs
window.setTimeout("closeHelpDiv();", 5000);

function closeHelpDiv(){
document.getElementById("helpdiv").style.display=" none";
}
</script>

Regards,
Erwin Moller
Oct 13 '06 #2
Erwin Moller wrote:
// close the div in 5 secs
window.setTimeout("closeHelpDiv();", 5000);

function closeHelpDiv(){
document.getElementById("helpdiv").style.display=" none";
}
Slightly cleaner, use a function reference rather than a string to
eval():

function closeHelpDiv(){
document.getElementById("helpdiv").style.display=" none";
}

// close the div in 5 secs
window.setTimeout( closeHelpDiv, 5000 );

Oct 13 '06 #3

"Phrogz" <ga***@refinery.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
// close the div in 5 secs
window.setTimeout( closeHelpDiv, 5000 );
From Javascript 1.3 spec:

"The expression must be quoted; otherwise, setTimeout calls it immediately."

nf
Oct 13 '06 #4
nutso fasst wrote:
"Phrogz" <ga***@refinery.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
// close the div in 5 secs
window.setTimeout( closeHelpDiv, 5000 );

From Javascript 1.3 spec:

"The expression must be quoted; otherwise, setTimeout calls it immediately."
Notice that I did not put parentheses on the end of that function name.
Thus, "calling it immediately" resolves the variable reference to the
function; setTimeout invokes that function object when required.

Oct 13 '06 #5

"Phrogz" <ga***@refinery.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Notice that I did not put parentheses on the end of that function name.
Thus, "calling it immediately" resolves the variable reference to the
function; setTimeout invokes that function object when required.
Ah, so. Thanks for edification.

Oct 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by salamol | last post: by
6 posts views Thread by Nicolae Fieraru | last post: by
2 posts views Thread by mja674s | last post: by
reply views Thread by Marcin Szarek | last post: by
4 posts views Thread by Claus Tondering | last post: by
5 posts views Thread by Susan Bricker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.