views:

63498

answers:

7

I am trying to have an element fade in, then in 5000 ms fade back out again. I know I can do something like:

setTimeout(function(){ $(".notice").fadeOut() }, 5000);

But that will only control the fade out, would I add the above on the callback?

+9  A: 

I just figured it out below:

$(".notice")
   .fadeIn( function() 
   {
      setTimeout( function()
      {
         $(".notice").fadeOut("fast");
      }, 2000);
   });

I will keep the post for other users!

Coughlin
yes, I think sending a callback is a better solution
Dan Beam
+4  A: 

You can do something like this:

$('.notice')
    .fadeIn()
    .animate({opacity: '+=0'}, 2000)   // Does nothing for 2000ms
    .fadeOut('fast');

Sadly, you can't just do .animate({}, 2000) -- I think this is a bug, and will report it.

strager
+49  A: 

Update: As of jQuery 1.4 you can use the .delay( n ) method. http://api.jquery.com/delay/

$('.notice').fadeIn().delay(2000).fadeOut('slow'); 

You could possibly use the Queue syntax, this might work:

jQuery(function($){ 

var e = $('.notice'); 
e.fadeIn(); 
e.queue(function(){ 
  setTimeout(function(){ 
    e.dequeue(); 
  }, 2000 ); 
}); 
e.fadeOut('fast'); 

}); 

or you could be really ingenious and make a jQuery function to do it.

(function($){ 

  jQuery.fn.idle = function(time)
  { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function()
         { 
            o.dequeue(); 
         }, time);
      });
  };
})(jQuery);

which would ( in theory , working on memory here ) permit you do to this:

$('.notice').fadeIn().idle(2000).fadeOut('slow'); 
Kent Fredric
I am wondering why you are using the queue when a simple usage of setTimeout will work as well.
SolutionYogi
because if you use the queue, its easy to add new events to and reuse the code, and code reuse is a GoodThing™
Kent Fredric
Note that, as also stated in the jQuery API documentation, delay() should really only be used for things related to the effects queue. If you need a timeout for something else, setTimeout() is still the way to go.
Scytale
+1  A: 

To be able to use it like that, you need to return this. Without the return, fadeOut('slow'), will not get an object to perform that operation on.

I.e.:

  $.fn.idle = function(time)
  {
      var o = $(this);
      o.queue(function()
      {
         setTimeout(function()
         {
            o.dequeue();
         }, time);
      });
      return this;              //****
  }

Then do this:

$('.notice').fadeIn().idle(2000).fadeOut('slow');
A: 

I have tried to do this so I can get a repetive flash but I cannot get the initial call do work.

I have included the new function

(function($){ 

  jQuery.fn.idle = function(time)
  { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function()
         { 
            o.dequeue(); 
         }, time);
      });

  };
})(jQuery);

` into the end of my jquery.js file (Ver. 1.3.2). However it doesn't work. I am trying to get this to flash the text. It comes back in "Web Developer" toolbar as it cannot find the function idle.

+1  A: 

Ben Alman wrote a sweet plugin for jquery called doTimeout it has a lot of nice features!

Check it out here: http://benalman.com/projects/jquery-dotimeout-plugin/

jason
+1  A: 

Useful info although does anyone have any suggestions how to have similar functionality using version 1.3.2 of JQuery?

Many thanks,

James Radford
Out of curiosity, why are you dependent on such an old version?
Benson