jQuery delay not working

Jquery

Jquery Problem Overview


$('.transparent').removeClass('transparent').delay(2000).addClass('not_transparent').delay(4000)

I have a div that is semi transparent and then want to switch it to not transparent. But the jQuery .delay(); method doesn't seem to work here. I've tried .fadeIn(); instead and that works with a delay but it doesn't work the changing classes.

Jquery Solutions


Solution 1 - Jquery

.delay() is used for items that are part of a queue, like animations. A simple addClass is not queued.

You could use setTimeout.

var trans = $('.transparent').removeClass('transparent');
setTimeout(function() {
    trans.addClass('not_transparent');
}, 2000);

As an alternative, you could add the non-queued item to the queue using .queue(), though I think a setTimeout would be better.

$('.transparent').removeClass('transparent').delay(2000).queue(function(nxt) {
      $(this).addClass('not_transparent');
      nxt();
});

Solution 2 - Jquery

I know this is an old question, but there's still a lot of traffic coming here from google so I'll add my two cents;

You could use something like -

$('.transparent').fadeIn().delay(500).queue(function(){
  $('.transparent').addClass('yourclass');
});

You can pass a function to the queue in order to execute them after the delay. Have been using this myself for very similar examples.

Solution 3 - Jquery

.delay() does not work with the .addClass() tag outside of a function so just use:

delay();
function delay(){
     $('.pgtitle').delay(5000).fadeIn(0).addClass('animated bounceInDown');
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthenengahView Question on Stackoverflow
Solution 1 - Jqueryuser113716View Answer on Stackoverflow
Solution 2 - JqueryLewisView Answer on Stackoverflow
Solution 3 - Jqueryyungtechboy1View Answer on Stackoverflow