jQuery : add and remove class at time interval using function setTimeout()

 

You can use jQuery standard functions to add and remove class at time interval using function setTimeout():
Please find the sample code below:

Add below jQuery link to your code to work:

https://code.jquery.com/jquery-3.3.1.js

After adding the above links, use the following:

/******Start of the code******/

$(“p”).on(“click”, function () {
//alert(‘click event called’);
var myElement = $(‘#myelement’);
myElement.addClass(‘classname-to-apply’);
setTimeout(function () {
myElement.removeClass(‘classname-to-apply’);
}, 5000);
});

/******End of the code******/

That’s it you are done!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s