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

73
Visualizações
call a method in a specific times in every seconds

I want to call a method 5 times in every one second. for example I have a methodA which i want to execute this method in 5 times in every 1 second. so how can i do it.

methodA(){
console.log("called")
}

timePeriod(){

setimeout....

}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let timePeriod = 1000  // 1000 ms
function methodA(){
  console.log("called")
}

setInterval(methodA, timePeriod)

notice here that we pass the function as an argument without () which in other words we're referencing the function. Note that if you pass the function name along with () it means you're calling it independently

Also, keep in mind that setInterval works a little bit different than you might think as it's a macro task asynchronous function in the event loop. if you don't know about the event loop simply all you need to know that setInterval doesn't actually call the function unless the rest of you script gets fired + the time period you specify

setInterval = the time needed to fire the rest of the script + the time period you specify

and what you can do to test this is:

const  tick  =  Date.now(),  timeLog  =  (val='---')  =>  console.log(`${val}  \n Elapsed: ${Date.now() -  tick} ms`)

setInterval(_ => timeLog('smth from setInterval'), 1000)

for(let i=1;i<1000000000;i++){
  // this loop will run for 1B times so that we get a clear time delay
}
timeLog('smth from normal console.log')

about 4 years ago · Juan Pablo Isaza Relatório

0

One could control both, the interval itself and also the condition for terminating the running interval, by writing a utility/helper function which enables not only clocked functions but also clocked methods.

function clocked(interval, isTerminate, proceed, target) {
  let count = 0;
  let intervalId = setInterval(() => {

    proceed.call(target ?? null);

    if (isTerminate(++count)) {
      clearInterval(intervalId);
    }
  }, interval);
}

const obj = {
  text: "The quick brown fox jumps over the lazy dog.",
  log() {
    console.log(this.text);
  },
};
function doTerminateWhen(counter) {
  return counter >= 5;
}

setTimeout(() => { console.log('1.001 seconds passed'); }, 1001);

clocked(200, doTerminateWhen, obj.log, obj);

setTimeout(() => { console.log('1 second passed'); }, 1000);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do it with setInterval()

const test = setInterval(() => console.log("Hello method"),1000);
setTimeout( () => clearInterval(test), 5000);

Hope it is what you need!

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