Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time between jQuery add/remove class

How can I set a timer, 10 sec in between this?

addClass('loading').removeClass('loading')

This is the full code

$("#loadmore").click(function() {
    cap += 10;
}).bind('click', loadfeed).addClass('loading').removeClass('loading');

Thank you.

like image 671
EnexoOnoma Avatar asked Sep 26 '11 03:09

EnexoOnoma


People also ask

How add and remove class in jQuery after some time?

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.

How do you remove a class after some time?

Free jQuery snippet to remove class from an element after delay of a few seconds. This snippet uses setTimeout function to delay the execution of the script. To remove class we simply use removeClass jQuery function.

How to remove class in JavaScript after some time?

removeClass() Method. This method removes one or more class names from the selected elements. If no parameter is specified in the removeClass() method, it will remove all class names from the selected elements.

What is addClass and removeClass in jQuery?

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.


1 Answers

Use setTimeout. Also not sure why you are binding to click twice in two different ways... so with those two changes it'd look something like this:

$("#loadmore").click(function() {
    cap += 10;
    loadfeed();
    $(this).addClass("loading");
    that = this
    setTimeout(function() {
        $(that).removeClass('loading');
    }, 10000)
});
like image 190
jhchen Avatar answered Oct 04 '22 03:10

jhchen