• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

215
Views
Add class after 2 second and remove after 7 second in loop

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>

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I suggest using CSS keyframes for this:

.video_title {
  color: red;
  animation: levelshow 7000ms infinite;
}

@keyframes levelshow {
  29.9% {
    color: red;
  }
  30% {
    color: green;
  }
  99.9% {
    color: green;
  }
  100% {
    color: red;
  }
}
<h2 class="video_title">Video title</h2>

Here is one way of doing it with JavaScript:

window.onload = () => {
  document.querySelector(".video_title").classList.remove("level_show")
  toggleSequence()
  setInterval(toggleSequence, 7000);
}

function toggleSequence() {
  setTimeout(() => {
    document.querySelector(".video_title").classList.toggle("level_show");
  }, 2000)
  setTimeout(() => {
    document.querySelector(".video_title").classList.toggle("level_show");
  }, 7000)
}
.video_title {
  color: red;
}

.video_title.level_show {
  color: green;
}
<h2 class="video_title">Video title</h2>

about 3 years ago · Juan Pablo Isaza Report

0

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>

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error