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();