Remove class with a fadeout after a Timeinterval is completed?

1.9k Views Asked by At

I've been able to remove a class with a setTimeout(function(), but now it just removes instantly, so I was wondering on how I can make it fadout(); aswell.

setTimeout(function(){
    $('.pagina-laden').removeClass('pagina-laden');
},2000);

.pagina-laden is the class where there is a background-image in css. But now I just need to fadeout out instead of abruptly vanishing. Thanks for the effort and help!

2

There are 2 best solutions below

4
On BEST ANSWER

try .delay() method

Heres a demo

$("#clickMe").click(function() {
  $("#fadeMeOut").delay(1500).fadeOut("slow");
});
#fadeMeOut {
  height: 300px;
  width: 300px;
  background: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<button id="clickMe">click me and wait</button>    
<div id="fadeMeOut"></div>

0
On

You can try below setTimeout function for that.

setTimeout(function () {
    $('.yourClass').fadeOut('slow', function(){
        $(this).remove();
    });
}, 3000);