SetTimeOut()
I am very excited about this setTimeOut , so will try this to oexplain clearly
Firstly , the simple defination of setTimeOut is that when we try to defer something then this is used , meaning when we try to do something after X amount of Time. Here is an example of the code
setTimeout(() => {
console.log(“Wake up!”);
}, 2000);
So here we have used the arrow function , if some of you don’t know arrow function , i will explain that later , but for know ,let’s focus on the syntax and the understanding of the code , the syntax is that we have used the keyword setTimeOut() , the code here means that the “Wake up!” will work after 2 seconds.
But there is a twist , that it is not confirmed , that it will work after 2 seconds , that is minimum , for that i will explain the functionality of setTimeOut , for that it includes the call stack , a web browser and a queue , i will explain that using this example :-
console.log(“There is a text that will be printed after some time and the time will be given in that “);
setTimeout(function(){
console.log(“Printed after 5 seconds due to the use of setTime Out Function “);
Get Kush Mantoo’s stories in your inbox
Join Medium for free to get updates from this writer.
} , 5000);
here we have set a timeout of 5 seconds and now the functionality , firstly the code goes from head to foot and firstly the console.log will be sent into the call stack and when it will be printed , it will be popped out of the stack , then after the setTimeout function will be sent into the call stack , then , here is the catch , it sends first this to the web browser and then pops out of the stack , , so the now the stack is empty and now the time of 5 seconds is running , then it is forwarded to the queue, now the queue will check if there is anything running in the stack , if it is running , then it will wait till the time the stack gets empty , that’s why i mentioned earlier , that the time is not guaranteed . And when the stack gets empty , then it will be forwarded to the stack and then the console.log will be printed .
Now i am sure you have understood this and there is ome more clear TimeOut which clears the timeOut , whicch we have set earlier .
Now their is a question what is the real life use of this :-
- Loading spinner
- Toast notification
- Auto logout
- Delayed popup
- Retry API





