-5

Possible Duplicate:
how to hide a div after some time period?

i need to hide a div (like mail sent successfull in gmail)after certain time period when i reload the page ? any body please help me by giving codes..

Community
  • 1
  • 1
rag
  • 515
  • 2
  • 7
  • 15
  • 7
    I am sooo tempted to tag this `plz-send-teh-codez` :) – Pekka Mar 11 '10 at 16:25
  • do you mean "after certain time period **or** when i reload the page" ? – N 1.1 Mar 11 '10 at 16:31
  • 5
    Folks, don't waste your time on this. The same guy asked the same question an hour ago, received *extensive* help by two people in the comments but didn't get anything he could copy+paste, so he decided to ask the same question again. What the f****. http://stackoverflow.com/questions/2426304/how-to-hide-a-div-after-some-time-period – Pekka Mar 11 '10 at 16:35

6 Answers6

3

Try this:

var timePeriodInMs = 4000;

setTimeout(function() 
{ 
    document.getElementById("myDiv").style.display = "none"; 
}, 
timePeriodInMs);
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
2
setTimeout(function(){
   document.getElementById('messageID').style.display = 'none';
}, 5000);  //5secs
N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • 1
    If I read him correctly, he wants persistence across page reloads. – Pekka Mar 11 '10 at 16:27
  • @Pekka: he says like gmail, in gmail, if we reload the page just after sending a mail, the "mail sent" notice does not persist. – N 1.1 Mar 11 '10 at 16:30
  • @nvl true, but he also says `after certain time period when i reload the page ? ` – Pekka Mar 11 '10 at 16:30
  • may be he missed " **or** " ? :) – N 1.1 Mar 11 '10 at 16:33
  • Untitled Document hiiiiiiiiiii

    not working pekka

    – rag Mar 11 '10 at 16:33
  • Folks, don't waste your time on this question. See my comment to the question above. – Pekka Mar 11 '10 at 16:35
  • got solution guys:-stackoverflow.com/questions/2426304/how-to-hide-a-div-after-some-time-period.... thanks for your kind support. regards, rag – rag Mar 11 '10 at 17:30
2

If you want the element to disappear after a certain time, regardless of page reloads (that's the way I read your question), you would have to work with cookies.

  • You would have to set a cookie with the starting point

  • You would have to execute a JavaScript function on page load that compares the time set in the cookie with the current time, and shows/hides the element accordingly.

  • For a perfect solution, you would additionally implement a timer that compares the current date to the date in the cookie every, say, half-second, and hides the element when the time has been reached.

Using a framework like JQuery is certainly a good idea for this.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • :) pekka pls give a solution.if anythng i need to clarify with my question pls ask me – rag Mar 11 '10 at 16:42
  • @rag it is rude in the *extreme* to open a question, receive *extensive* support there much beyond normal level, and then when no reaction comes for 20 minutes to open *the same question again*. You are showing zero respect for your fellow programmers' time and attention - most of whom have work to do - and you can be sure I will not be putting one minute into your questions again. Go learn it yourself. – Pekka Mar 11 '10 at 16:45
  • pekka i have tried it in javascript becuase i need to find eithr der should be solution.. – rag Mar 11 '10 at 16:46
  • @rag then go and hire somebody to solve it for you. – Pekka Mar 11 '10 at 16:46
  • ok pekka i let me analyse those supports...thanks guys am leavng thanks pekka.. – rag Mar 11 '10 at 16:48
  • pekka i got solution from my jquery posts...hi:) thanks&regards :rag – rag Mar 11 '10 at 17:20
1

You may use setTimeout to delay a function's execution:

window.setTimeout(doSomething, 1000); // 1000ms == 1 second

To hide an element, you may set its display property to none:

var element = document.getElementById('foo');

function doSomething() {
    element.style.display = 'none';
}
moff
  • 6,415
  • 31
  • 30
1

You are looking at the setTimeout( expression, timeout ); where you need to give it an expression to run after the timeout in milliseconds that you allocate to it. then you would do element.style.display="none"

ex:

setTimeout( function(){element.style.display="none"}, 4000 );
Jonathan Czitkovics
  • 1,642
  • 11
  • 11
0
  1. add jquery to your html document
  2. add script tag to head of your document containing:
<script type="text/javascript">
    $(document).ready(function(){
        window.setTimeout(function(){
            $('#id-of-div').hide();
        }, 10000 /* delay time in milliseconds */
    });
</script>
Kris
  • 40,604
  • 9
  • 72
  • 101
  • Untitled Document hiiiiiiiiiii

    i have tried this kris but not working

    – rag Mar 11 '10 at 16:28
  • @rag have you added JQuery to the document? – Pekka Mar 11 '10 at 16:30
  • That makes me think jquery didn't actually load for that page, or that your documents parse tree is somehow broken. what does the javascript console say? (firebug or webkit inspector available?) – Kris Mar 11 '10 at 16:31
  • 1
    One does not need a whole 26kb JQuery library to do this. What if he does not want a to use a library? What if he uses Mootools? – Jonathan Czitkovics Mar 11 '10 at 16:32
  • and tried ur $(document).ready(function(){ window.setTimeout(function(){ $('#id-of-div').hide(); }, 10000 /* delay time in milliseconds */ }); ------------->not working – rag Mar 11 '10 at 16:34
  • @Jonathan, then he should disregard my answer and go with another solution of the readily available ones. – Kris Mar 11 '10 at 16:34
  • Folks, don't waste your time on this guy. See my comment to the question above. – Pekka Mar 11 '10 at 16:34
  • Thats because he/she expects you to install JQuery just to do this. – Jonathan Czitkovics Mar 11 '10 at 16:35
  • /jqury/jquery-1.4.2.min.js:77Uncaught SyntaxError: Unexpected identifier and timehidediv.php:12Uncaught SyntaxError: Unexpected token } are the errors – rag Mar 11 '10 at 16:36
  • you have a typo somewhere before line 13 of the html output – Kris Mar 11 '10 at 16:38
  • Its because he made no mention of using JQuery so I don't see it as a valid answer. – Jonathan Czitkovics Mar 11 '10 at 16:44
  • got solution guys:-stackoverflow.com/questions/2426304/how-to-hide-a-div-after-some-time-period.... thanks for your kind support. regards, rag – rag Mar 11 '10 at 17:24
  • jQuery not necessary for this one. – Harlin Oct 15 '20 at 02:25
  • @Harlin: ofcourse not and nowadays it would be pretty weird to add jquery to anything for almost any reason but the landscape was very different in 2010, jquery was a great equalizer that was almost essential to support more than one browser at a time. – Kris Oct 16 '20 at 09:01