Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda