如何在不影响 setInterval 内部的情况下每 x 秒更新一次 setInterval 数据?

How to update setInterval data every x seconds without affecting inside setInterval?

setInterval(() => {
  let type = ['works', 'testing'];
  let ranType = type[Math.floor(Math.random() * type.length)];

  if (ranType === 'works') {
    setInterval(async () => {
      console.log(`Okay Working ${RANDOM DATA}`);
    }, 10000);
  } else {
    setInterval(async () => {
      console.log(`Testing also working ${RANDOM DATA}`);
    }, 10000); // But RANDOM DATA should update every 10 seconds - I have RANDON data
  }
}, 30000); // ranType should update every 30 seconds

After 30 seconds
if ranType is 'works'
it should repeat console with random data every 10 seconds

Again after 30 seconds it (ranType) should change to testing
if ranType is 'testing'
it should repeat console with other random data every 10 seconds

const ranType_reset_period = 30000;
const works_reset_period = 10000;
const testing_reset_period = 10000;
const random_data_reset_period = 10000;

let works_interval = -1;
let testing_interval = -1;

let RANDOM_DATA = "RANDOM_DATA_DEFAULT";

function reset_random_data() {
  let sample_random_data = ['RANDOM_DATA_1', 'RANDOM_DATA_2', 'RANDOM_DATA_3', 'RANDOM_DATA_4'];
  RANDOM_DATA = sample_random_data[Math.floor(Math.random() * sample_random_data.length)];
  console.log('RANDOM_DATA', 'RESET', RANDOM_DATA);
}

function get_random_ranType() {
  let type = ['works', 'testing'];
  return type[Math.floor(Math.random() * type.length)];
}

function reset_works_interval() {
  console.log(`Okay Working ${RANDOM_DATA}`);
}

function reset_testing_interval() {
  console.log(`Testing also working ${RANDOM_DATA}`);
}

function reset_rantype() {
  clearInterval(works_interval);
  clearInterval(testing_interval);

  if (get_random_ranType() === 'works') {
    works_interval = setInterval(reset_works_interval, works_reset_period);
  } else {
    testing_interval = setInterval(reset_testing_interval, testing_reset_period); // But RANDOM DATA should update every 10 seconds - I have RANDON data
  }
}

setInterval(reset_random_data, random_data_reset_period);
setInterval(reset_rantype, ranType_reset_period);

插图

const ranType_reset_period = 30000;
const works_reset_period = 10000;
const testing_reset_period = 10000;
const random_data_reset_period = 10000;

let works_interval = -1;
let testing_interval = -1;

let RANDOM_DATA = "RANDOM_DATA_DEFAULT";

function reset_random_data() {
  let sample_random_data = ['RANDOM_DATA_1', 'RANDOM_DATA_2', 'RANDOM_DATA_3', 'RANDOM_DATA_4'];
  RANDOM_DATA = sample_random_data[Math.floor(Math.random() * sample_random_data.length)];
  console.log('RANDOM_DATA', 'RESET', RANDOM_DATA);
}

function get_random_ranType() {
  let type = ['works', 'testing'];
  return type[Math.floor(Math.random() * type.length)];
}

function reset_works_interval() {
  console.log(`Okay Working ${RANDOM_DATA}`);
}

function reset_testing_interval() {
  console.log(`Testing also working ${RANDOM_DATA}`);
}

function reset_rantype() {
  clearInterval(works_interval);
  clearInterval(testing_interval);

  if (get_random_ranType() === 'works') {
    works_interval = setInterval(reset_works_interval, works_reset_period);
  } else {
    testing_interval = setInterval(reset_testing_interval, testing_reset_period);
  }
}

setInterval(reset_random_data, random_data_reset_period); // But RANDOM DATA should update every 10 seconds - I have RANDON data
setInterval(reset_rantype, ranType_reset_period);

好的,我想这就是你想要的,如果不是预期的结果请告诉我。每 30 秒外层 setInterval 清除最后一个 intervalId 并创建一个新的。

    let intervalId;

    setInterval(() => {
      let type = ["works", "testing"];
      let ranType = type[Math.floor(Math.random() * type.length)];

      console.log("new cycle of interval of 30 seconds");

      clearInterval(intervalId);

      setTimeout(() => {
        if (ranType === "works") {
          intervalId = setInterval(async () => {
            console.log(`Okay Working`);
          }, 10000);
        } else {
          intervalId = setInterval(async () => {
            console.log(`Testing also working`);
          }, 10000);
        }
      });
    }, 30000);

编辑

创建了一个可能更清晰的新版本

    const setIntervalXTimes = (callback, delay, repetitions) => {
      let count = 0;
      let intervalID = setInterval(() => {
        if (count === repetitions) {
          clearInterval(intervalID);
        } else {
          callback();
        }
        count++;
      }, delay);
    };

    setInterval(() => {
      let type = ["works", "testing"];
      let ranType = type[Math.floor(Math.random() * type.length)];

      if (ranType === "works") {
        setIntervalXTimes(() => console.log(`Okay Working`), 1000, 3);
      } else {
        setIntervalXTimes(() => console.log(`Testing also working`), 1000, 3);
      }
    }, 3000);