Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove class after 3 seconds

I'd like to put a timeout function to remove these two classes, but I have no idea how to do that. Can anyone help me how to include a timeout here? Thanks in advance.

.done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text('Message sent!');

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
            //$('#budget').val('');
        })
like image 593
Lucas Haas Avatar asked Oct 16 '14 16:10

Lucas Haas


People also ask

How do I remove a class from an element using jQuery?

If you have it applied to an element but want to remove it from an element after a designated amount of time has passed, you can do so by using jQuery's .setTimeout () method. var header = $ ('#header'); header.addClass ('blue'); setTimeout (function () { header.removeClass ('blue'); }, 4000);

How do I remove a class from the Me page?

Click the button to remove a class from me! Remove Class Step 1) Add HTML: In this example, we will use a button to remove the "mystyle" class from the <div> element with id="myDIV": Example

How do I remove the Blue class from the header element?

The first line of the snippet establishes the variable header (var header will select the #header element), and the next line adds the blue class to the header element. Then, the .setTimeout () method creates a function that will remove the blue class from the header element after 4000 milliseconds.

How to remove the'mystyle'class from the <div> element?

Step 1) Add HTML: In this example, we will use a button to remove the "mystyle" class from the <div> element with id="myDIV": Example <button onclick="myFunction()">Try it</button>


2 Answers

maybe something like...

 setTimeout(function(){
            $(formMessages).removeClass('error');
            //....and whatever else you need to do
    }, 3000);
like image 89
prawn Avatar answered Oct 02 '22 12:10

prawn


Use queue method (https://api.jquery.com/queue/) to queue your function calls in combination with delay.

$(formMessages)
  .addClass('error')
  .delay(3000)
  .queue(function(next){
    $(this).removeClass('error');
    next();
  })
like image 31
Aditya Kapoor Avatar answered Oct 02 '22 11:10

Aditya Kapoor