tags:

views:

520

answers:

4

Something as simple as:

$("#div").addClass("error").delay(1000).removeClass("error");

doesn't seem to work. What would be the easiest alternative?

+1  A: 

What follows is somewhat hacky, but you could create a delay with a chained animate that changes nothing. Eg:

$("#div").addClass("error").animate({opacity:1.0}, 1000).removeClass("error");

which takes 1 second to turn the opacity from, well, 1.0 to 1.0, then fires removeClass()

(Not checked the code above - look at the docs for animate())

stevejalim
This doesn't work, behaves similar to delay() - calls removeClass immediately.
serg
This doesn't work because all animate() does is add to the queue and then move to the next item in the chain, it doesn't wait till the animation is complete, for that you would have to use the callback.
PetersenDidIt
A: 

Delay operates on a queue. and as far as i know css manipulation (other than through animate) is not queued.

prodigitalson
+3  A: 

AFAIK the delay method only works for numeric CSS modifications.

For other purposes JavaScript comes with a setTimeout method:

window.setTimeout(function(){$("#div").removeClass("error");}, 1000);
Jasper
+1: Don't use Jquery for Jquery's sake. There are simple javascript solutions to many problems.
Joel Potter
+3  A: 

You can create a new queue item to do your removing of the class:

$("#div").addClass("error").delay(1000).queue(function(next){
    $(this).removeClass("error");
    next();
});
PetersenDidIt
My thought exactly :p
Romuald Brunet