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

230
Views
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
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!