Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Im trying to make a schedule to change who is doing a task every 7 days. But im very new to js and am stuck

I’m trying to make a schedule to change who is doing a task every 7 days. I need it to change a html element class and repeat every Friday. Thanks in Advance.

var date = new Date();
  var dayOfWeek = Date.getDay(); // 0 is Sunday, 1 is Monday, etc...
  var kestrals = document.getElementById("k");
  var eagles = document.getElementById("e");
  var merlins = document.getElementById("m");
  var currentDuty
  var intervalID = window.setInterval(changeDuty, 10000);

  function changeDuty() {
    if(dayOfWeek=5) {
        //Dont know what do do here
    }

  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

One solution might be to use the classList property of the element.

Example:

const kestrals = document.getElementById("k");
const eagles = document.getElementById("e");
const merlins = document.getElementById("m");

let currentDuty = kestrals; // initial duty value

function changeDuty() {
  const today = new Date();
  const dayOfWeek = today.getDay(); // 0 is Sunday, 1 is Monday, etc.

  if (dayOfWeek === 5) { // 5 - Friday
    switch (currentDuty) {
      case kestrals:
        toggleDuty(kestrals, eagles);
        currentDuty = eagles;
        break;
      case eagles:
        toggleDuty(eagles, merlins);
        currentDuty = merlins;
        break;
      case merlins:
        toggleDuty(merlins, kestrals);
        currentDuty = kestrals;
        break;
      default:
        toggleDuty(kestrals, eagles);
        currentDuty = eagles;
        break;
    }
  }
}

// toggle duty class on elements
function toggleDuty(offDuty, onDuty) {
  offDuty.classList.remove("duty");
  onDuty.classList.add("duty");
}

// set the initial value of the duty class for the element
kestrals.classList.add("duty");

// call changeDuty() every week
const oneWeek = 7 * 24 * 60 * 60 * 1000;
const intervalID = setInterval(changeDuty, oneWeek);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading