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

Hi Guys,
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!

Leave a Reply