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

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

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 Report

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 Report

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