vinayofficial vinayofficial - 5 months ago 51x
Javascript Question

Add then remove class after delay but without page refresh

I am adding a class

on click and now after time interval of 2 seconds I want to remove this class, but without page reload/refresh.I used
in jQuery as:


but it's not working.

Then I tried

It's working, but reloading page. I want something that would add, and then remove, class after specific time but do not refresh/reload the page.

Please help, and thank you in advance.


Using jquery and setTimeout function:

var $elm = $("#username").addClass("error");
setTimeout(function() {
}, 2000);
<script src=""></script>
<div id="username">USER NAME</div>