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

108
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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