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

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

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 Denunciar

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 Denunciar

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