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('');
})
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);
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
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.
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>
maybe something like...
setTimeout(function(){
$(formMessages).removeClass('error');
//....and whatever else you need to do
}, 3000);
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();
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With