Afshin
Afshin

Reputation: 2437

addClass for some moments and the remove it using javascript

I want to add class to a div and remove it after some moments. I used setTimeout and I don't really know that if I'm right. The first part, I mean addClass works perfectly until I add removeClass inside the setTimeout method. So, here it goes my code:

$.ajax({
        url: "<?php echo site_url('main/order') ?>",
        type: 'POST',
        data: form_data, // $(this).serialize(); you can use this too
        dataType: 'json',
        success: function(res) {
            if(res) {
                classie.addClass( theForm.querySelector( '.simform-inner' ), 'hide' );
                var messageEl = theForm.querySelector( '.final-message' );
                messageEl.innerHTML = res;
                classie.addClass( messageEl, 'show' );

                setTimeout(classie.removeClass( messageEl, 'show' ), 5000);
                setTimeout(classie.removeClass( theForm.querySelector( '.simform-inner' ), 'hide' ), 6000);

            }
        }

   });

What am I doing wrong?!

Upvotes: 1

Views: 132

Answers (1)

winhowes
winhowes

Reputation: 8065

Change your setTimeout's to this:

setTimeout(function(){
    classie.removeClass( messageEl, 'show' )
}, 5000);
setTimeout(function(){
    classie.removeClass( theForm.querySelector( '.simform-inner' ), 'hide' )
}, 6000);

You need to pass a function into setTimeout() and in your code you were calling a function and passing the result in instead of passing the function itself.

Upvotes: 1

Related Questions