Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!