Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda