Is it possible to add a delay to the remove class part of toggleClass only?
In the fiddle, I would like the red div to turn green instantly when clicked. But it should delay in turning back to red when clicked again.
http://jsfiddle.net/simbasounds/ggk8xxya/1/
HTML
<div class="container">
I am a div
</div>
CSS
.container {
padding: 10px;
background: red;
}
.container.green {
background: green;
}
jQuery
$(".container").click(function(){
$(this).toggleClass('green');
});
For Simple Time Delay while converting from green to red
$(".container").click(function(){
var $element = $(this);
//check if element has green class --> remove green class with a delay
if ($element.hasClass('green')) {
var delay = 1000; //ms
setTimeout(function(){
$element.removeClass('green');
},delay );
}
// element doesn't have green class --> add it
else {
$element.addClass('green');
}
});