I want to add a class after 2 seconds and remove it after 7 seconds and when remove again add the class after 2 seconds and remove it after 7 seconds
$(document).ready(function() { function removeAddClass1() { $(".video_title").removeClass("level_show"); setTimeout(removeAddClass1, 7000); } removeAddClass1(); function removeAddClass2() { $(".video_title").addClass("level_show"); setTimeout(removeAddClass2, 2000); } removeAddClass2(); });
.video_title { color: red; } .video_title.level_show { color: green; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h2 class="video_title">Video title</h2>
Advertisement
Answer
This is done by javascript
and jQuery
without using CSS keyframes
.
// for switch class const switchCls = (selector, cls) => $(selector).toggleClass(cls); // delay and do task const delay = (action, ms) => { return new Promise((resolve) => { const newAction = () => { action(); resolve(action); }; setTimeout(newAction, ms); }); }; // ensure execution order const task = (selector, cls) => { const switchFn = () => switchCls(selector, cls); delay(switchFn, 2000).then(action => delay(action, 7000) ).then(action => task(selector, cls) ); }; // start execution task('.video_title', 'level_show'); // screen timer let second = 0; setInterval(() => { $('h1').text(`${++second} second`); }, 1000);
.video_title { color: red; } .video_title.level_show { color: green; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h2 class="video_title">Video title</h2> <h1>0 second</h1>