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

152
Visualizações
when is the callback passed to setTimeout put on the message queue?
setTimeout(callback, 1000)

Is callback put on the message queue after 1000ms or is it put to the message queue immediately?

If it is put to the message queue after 1000ms, then does that mean that the 1000 does not mean that the callback will run in 1000, but instead it means that the callback may be run at a minimum only after 1000?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Is callback put on the message queue after 1000ms or is it put to the message queue immediately?

After 1000ms - that's when the timer runs out. If it was put on the message queue immediately, there would be no waiting before is run.

If it is put to the message queue after 1000ms, then does that mean that the 1000 does not mean that the callback will run in 1000, but instead it means that the callback may be run at a minimum only after 1000?

Yes, if the event loop is still busy with other tasks (especially long-running blocking code) at that time, it will have to wait for those to finish. The message queue is a queue, it is serviced one task after the other; no two callbacks are executed in parallel.

(Notice that setTimeout is allowed to lag arbitrarily anyway).

about 4 years ago · Juan Pablo Isaza Relatório

0

It's really doesn't matter when the callback is added to the queue, because setTimeout is not true async anyway (aka it still runs on the same thread as the rest of the code).

Here is a little example showing how setTimeout is just a suggested minimum wait timeout:

let time = new Date().getTime();
setTimeout(() => test(1), 1000);
setTimeout(() => test(2), 1000);
setTimeout(() => test(3), 1000);
setTimeout(() => test(4), 1000);

function test(id)
{
  console.log(`executed ${id} after `, new Date().getTime() - time);
  for(let i = 0; i < 1000000000; i++)
  {
    i * 9;
  }
}

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