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

182
Views
Execute a function for x time

I'm looking for execute a function on nodejs with express for an exact time, and if the time have pass but the function don't end, the function stop.

I tried with setTimeOut. But it just works for execute a function after "x" time. I just need to execute the function for "x" time.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could set a Boolean variable and change its value after "x" of time, while checking the same variable in your function.


var Continue = true; // Our Boolean variable.
var x = 5000; // Execute the code for amount of milliseconds.

function canContinue(){
    return Continue;
}

var ourInterval = setInterval( function () {
    if(canContinue())
        console.log("Continue the code for one more second.")  
    else
        clearInterval( ourInterval )
}, 1000 );

setTimeout( function () {
    Continue = false;
}, x);

Hope that helps.

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!