In my Rails application, I send an Ajax request when the user hits the Save button, when it sends the request, I can return some jQuery.
What I'd like to do is add a class (saving), have a delay and then remove the class.
So, I added this:
$('.button').addClass('saving').delay(2000).removeClass('saving');
For some reason, it isn't working. What am I doing wrong?
Similarly, you can remove the classes from the elements using the jQuery removeClass() method. The removeClass() method can remove a single class, multiple classes, or all classes at once from the selected elements.
setTimeout() method creates a function that will remove the blue class from the header element after 4000 milliseconds.
jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
.delay() is actually for animations.
Use setTimeout()
$('.button').addClass('saving');
setTimeout(function () {
$('.button').removeClass('saving');
}, 2000);
delay
only works with animation-related methods, you can use queue
method:
$('.button').addClass('saving').delay(2000).queue(function( next ){
$(this).removeClass('saving');
next();
});
http://jsfiddle.net/Rp6Xw/44/
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