86

I am building a interstitial page, using <div> and JavaScript, really simple script but neat.

Everything is working, but I also would like to close the div's after a few seconds (like 10 seconds, for example). Here what I have so far:

  • I have two div's, 1 and 2.
  • I have the CSS setup for div 1 like: display: none; (div 1 have the content for the splash screen)
  • Div 2 is the layer that will cover the page and leave only div 1 visible.

To load the div's I have a onload function like this:

onload="document.getElementById('div1').style.display='block';document.getElementById('div2').style.display='block'"

To hide the divs I have an onclick function like this:

<a href = "javascript:void(0)" onclick = "document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'"></a>

How can I execute the onclick function with a timer, and how can I do it? It also has to be in JavaScript.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
asked Nov 24, 2011 at 5:35
0

3 Answers 3

141

I believe you are looking for the setTimeout function.

To make your code a little neater, define a separate function for onclick in a <script> block:

function myClick() {
 setTimeout(
 function() {
 document.getElementById('div1').style.display='none';
 document.getElementById('div2').style.display='none';
 }, 5000);
}

then call your function from onclick

onclick="myClick();"
str
45.5k18 gold badges117 silver badges134 bronze badges
answered Nov 24, 2011 at 5:38
Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand, you just said you are doing it in onclick? Are we missing something?
Second. how can I execute the function myClick without have to use the onclick, onload, etc. I would like to start the function at the same time O load the code
old question but best solution is - insert ``` myclick();``` also in script tag this will automatically trigger myclick function as soon as document started loading and as defination of function it will be executed after 5 second
15

setTimeout will help you to execute any JavaScript code based on the time you set.

Syntax

setTimeout(code, millisec, lang)

Usage,

setTimeout("function1()", 1000);

For more details, see http://www.w3schools.com/jsref/met_win_settimeout.asp

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Nov 24, 2011 at 5:40

1 Comment

Note that using the overload to provide the function using a string is considered bad practice. You're better off using setTimeout(function1, 1000); or setTimeout(function() { function1(); }, 1000); or setTimeout(() => function1(), 1000); depending on the nature of this references in function1. Also, I prefer using MDN's setTimeout documentation.
5
onclick = "setTimeout(function() { document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'}, 1000)"

Change 1000 to the number of milliseconds you want to delay.

answered Nov 24, 2011 at 5:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.