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

231
Visualizações
Why or How does setTimeout run once when called directly but run in a loop when called from within a function?

This question is related to SetTimeout is confusing me and I need to be able to promisify it as I am trying to understand the basics of setTimeout.

When I call setTimeout as below (directly from outside) it runs just once and this is clear to me as it's what the definition of setTimeout says

A function to be executed after the timer expires.

let i = 0;
let str = "Alim";

function type() {
  console.log(str, str.length, i);
}
type(); // runs immediately

setTimeout(type, 3000); // runs after 3 secs

But when I call it from within a function (or as some say - call it from within itself) then it runs in a loop every 3 secs.. but why and how? I can't get my head around this and trying more complex stuff without understanding the basics might not be a good idea for me..

Why does the setTimeout run again and again in the code below when it's actually called just once from outside by typing type()


let i = 0;
let str = "Alim";

function type() {
  console.log(str, str.length, i); // runs immediately
  setTimeout(type, 3000); // runs after 3 secs and again and again and again.....!!!
}
type(); 

about 4 years ago · Juan Pablo Isaza
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