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

106
Views
How to call the setTimeout?

I am being given the below mentioned code and asked to print the console after 3 seconds - B and C after A.

I tried

  setTimeout(main(),3000);

But I get no output.

function sleep(){

}

function main(){

console.log('A');

// call after 3 sec
console.log('B');


// call after 3 sec
console.log('C');

}
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

setTimeout(main , 3000);

remove function call '( )'

How setTimeout works: You specify the function (as a first argument) to be called after your timeout (second argument: 3000)

about 4 years ago · Juan Pablo Isaza Report

0

Generally you should pass the function by name to the setTimeout function without calling it.

setTimeout(main,3000);

about 4 years ago · Juan Pablo Isaza Report

0

setTimeout(main(), 3000) will execute main immediately because you put the () after it. Just use the name. setTimeout(main, 3000)

Also, you'll want to put your timeout on the two specific log statements you want to call later, not the whole function, or you could also log A outside of the main function and then call the main function after the timeout.

function main() {

  console.log('A');

  // call after 3 sec
  setTimeout(() => {
    console.log('B');
    console.log('C');
  }, 3000);

}

main();

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!