Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  40] [ 1]  / answers: 1 / hits: 18508  / 7 Years ago, fri, may 26, 2017, 12:00:00

I want to add and remove a class to a div on the basis of time. It should add a class after 6 secs and remove it after 4 secs. I tried a basic implementation. why isn't this working? I assume the problem is two setTimeouts together like this. If I comment out the second line the first one works. What is happening here?



setTimeout(addHighlight(), 6000);
setTimeout(removeHighlight(), 10000);


Can somebody show me how i can write a single function that does both(add and remove class) on the basis of a delay parameter?


More From » jquery

 Answers
32

Try this:



setTimeout(function() {
$('#square').addClass('highlight');
}, 6000);

setTimeout(function() {
$('#square').removeClass('highlight');
}, 10000);


https://jsfiddle.net/js6wh78h/8/


[#57653] Wednesday, May 24, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
terrellhunterm questions
Mon, Aug 31, 20, 00:00, 4 Years ago
Mon, Aug 5, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Mon, Mar 11, 19, 00:00, 5 Years ago
;