Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
How to update setInterval data every x seconds without affecting inside setInterval?
  • How can i update ranType every 30 seconds.
  • since ranType is random if ranType === 'works' how to add interval to update inside data every 10 seconds.
  • This is what i tried:
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
  • expected output:

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

  • Other issues is its keep repeting 3-4 times every 10 seconds.
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);

Illustration

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

Ok, I think this is what you want, please let me know if is not the expected result. Every 30 seconds the outer setInterval clears the last intervalId and creates a new one.

    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);

EDIT

created a new version which might be clearer

    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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda