Beruflich Dokumente
Kultur Dokumente
log in
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
I was wondering, how in jquery am I able to hide a div after a few seconds? Like Gmail's messages for
example.
I've tried my best but am unable to get it working.
javascript
jquery
timing
Is just hiding good enough, or do you need it to fade? Joel Coehoorn May 4 '09 at 17:03
add a comment
6 Answers
This will hide the div after 1 second (1000 milliseconds).
setTimeout(function() {
$('#mydiv').fadeOut('fast');
}, 1000); // <-- time in milliseconds
If you just want to hide without fading, use hide() .
answered May 4 '09 at 17:03
swilliams
20.3k 13 64 114
and you have beaten crazy Joel Coehoorn very nicely in one shot! :) Cawas Mar 18 '11 at 19:14
@James, For sure there is no difference in the final result, but I suppose that implementation using
.delay() is more "native" and elegant for jQuery . Paul T. Rawkeen Aug 14 '12 at 9:50
you can replace .fadeOut('fast') with .hide() for instant hide of the div. Raptor Jan 10 '13 at 2:29
add a comment
This is better beacause I don't have to use setTimeoutand code is easier to read. Marek Bar Oct 7 '12 at
15:58
tour
help
add a comment
Probably the easiest way is to use the timers plugin. http://plugins.jquery.com/project/timers and then call
something like
$(this).oneTime(1000, function() {
$("#something").hide();
});
answered May 4 '09 at 17:05
stimms
15.5k 15 58 105
Is there any compelling reason to use the timers plugin over setTimeout or setInterval? spender May 4 '09 at
17:06
I would say that downloading and attaching a jquery plugin is less easy than simply using setTimeout.
Nathan Ridley May 4 '09 at 17:06
I don't think this is necessarily a bad thing, but since it is rare that I ever use timers in my code, having that
plugin around (read: extra code, bloat) for those few times does not outweigh the cost. If you were writing a lot
of code that needed to use timers, and were using jQuery, I can see why this plugin would be very useful to
keep with the jQuery syntax... Jason Bunting May 4 '09 at 17:39
add a comment
Using the jQuery timer will also allow you to have a name associated with the timers that are attached to
the object. So you could attach several timers to an object and stop any one of them.
$("#myid").oneTime(1000, "mytimer1" function() {
$("#something").hide();
}).oneTime(2000, "mytimer2" function() {
$("#somethingelse").show();
});
$("#myid").stopTime("mytimer2");
The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the
JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of
extremely bad coding. The eval function is the most misused feature of JavaScript.
http://www.jslint.com/lint.html
edited Oct 10 '11 at 8:49
kamaci
11.4k 21 99 213
add a comment
Not the answer you're looking for? Browse other questions tagged javascript jquery
timing or ask your own question.