tags:

views:

7246

answers:

8

Is there any way to fadeout a div after 5 Seconds without using a setTimeOut function?

A: 

Assuming you mean 'wait five seconds and then fade out', I think you'll have to use a plugin to force the delay, eg this one

Stuart Dunkeld
+1  A: 

Not sure if you want it to take 5 seconds or start in 5 seconds.

For it to take 5 seconds: The jQuery fadeout function can be used on a div, and it will reduce the element's opacity until it is 0 and then display none the div. The speed of the fade is a parameter for the function.

http://docs.jquery.com/Effects/fadeOut#speedcallback

To start it in 5 seconds, you'll need some sort of timer that starts when the document or window is ready, or when the div is ready depending on what you want.

danivovich
+9  A: 

How about the fadeOut() function. Would look something like this:

$("#myDiv").fadeOut(5000);
ckramer
This doesn't really answer the question. He wants to fade *after* 5 seconds, not have the fade last *for* 5 seconds.
James Burgess
i want to wait 5 seconds before the animation starts. It's solved, I wrote function as $("div").fadeOut(10000); then it is working as what i want to do exactly with out using setTimeOut() function.Thank you!
Srikanth
+17  A: 

Case 1: if you want to start fadeOut after 5 seconds, use this:

jQuery.fn.delay = function(time,func){
    return this.each(function(){
     setTimeout(func,time);
    });
};

Then, use it like this:

$('#div').delay(5000, function(){$(#div').fadeOut()})

You can't achieve this without using setTimeOut at all

Case 2: if you want the duration of fadeOut to be 5 seconds, use this:

$('#div').fadeOut(5000)
Ionut Staicu
Very good and useful :) just what i was after! Cheers!
Shadi Almosri
+1  A: 

// i use this pause plugin i just wrote

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

Call it like this :

$("#mainImage").pause(5000).fadeOut();

Note: you don't need a callback.

Simon_Weaver
for some reason pause(5000).css("opacity", .5) doesn't pause before setting the opacity, but it works for fadeout. anyone care to explain? note: when i said 'plugin i just wrote' i was trying to indicate 'not fully tested' - but it should work for what was asked for
Simon_Weaver
css() doesn't use the animation queue. if you want to pause before changing opacity you'd need something like "pause(5000).animate({ 'opacity': 0.5 });
mishac
A: 

hi, im new to jquery and want to do the same.. i notice your answers here but does anyone know how to actually put that in your pages, e.g. where each line goes inthe js or html files?

thanks Emma

write the code in $(document).ready(function(){ // code here});
Srikanth
+4  A: 

I just had the same problem and in my opinion the marked answer doesn't actually really satisfy the question. If one specifies it like

$("#myDiv").fadeOut(5000);

as suggsted, the fading process itself will last for 5 seconds, but not start after 5 seconds.

So I was searching for an alternative, without having to include another jQuery plugin etc. The simplest solution I came up with was to write it as follows:

$("#myDiv").fadeTo(5000,1).fadeOut(1000);

It uses the fadeTo effect and it is somehow a "hack". I let the fadeTo run for 5 seconds and let it fade to 1 = 100% opacity. In this way the user doesn't perceive any change. Afterwards the normal call to fadeOut with a duration of the effect of 1 second.

I guess this solution is quite simple since it doesn't require any additional plugin and can be written in 1 line.

Cheers.

//EDIT:
Apparently there is now the possibility to do something like this:

$('#myDiv').delay(800).fadeOut(1000);

Here are some more cool, useful functions.

Juri
+5  A: 

everyone knows that in jquery 1.4 there's a delay function now, right?

$('#div').delay(5000).fadeOut(400)

that's how you do it, without having to add any custom functions or plug-ins. it's native to jquery 1.4

ExodusNicholas