Hi, I have already asked that question in the other forum of Javascript live but I still don't understand. I have a question about the setTimeout function and I will be happy to get an answer. I don't really get the delay thing (the second argument the function gets). In the article it says if I'm not mistaken that as soon as you call the setTimeout function the timer starts to count down, but it already says that if the countdown reaches 0 and the program is busy at that time doing something else ,the execution of the scheduled code will delay some more until the program gets free from what it does or until it finishes what it is currently does.

I don't quite understand what it means until the program finishes what it does or the program is in the middle of doing something else ,is it a statement it needs to finish first? a function that is currently runs that needs to finish execute before the program can moves on to the scheduled code(that his timer is already reaches 0 a while ago)?

In return I got an answer from the user "felgall" tried to explain it to me, but his answer confused me even more. Can someone please explain me his answer?

A computer processor can only execute a set number of commands at the same time. For most computers in the past that was one instruction at a time, now it is common to find processors that can process two or sometimes even more things at the same time.

Basically when the computer is performing tasks it has an input queue of instructions that are waiting to be processed and as it finishes processing each instruction iit then gets the next one off the queue. If you tell the computer to run something right now then that instruction gets added to the end of the queue and will wait its turn to run after anything else already in the queue. When a timeout timer finishes counting down to zero it adds the code it has been told to run onto the end of the queue no differently than any other code that is supposed to run right now and the commands will get run when the processor gets to them.

One thing you need to remember is that ALL of the programs running on your computer are adding commands to be run onto the queue all the time and so how soon after you tell something to run that it actually does will depend on how much is on the queue at that time.

Every time you start something asynchronously such as through a timeout or ajax call in JavaScript you add additional tasks to what is running that will eventually add more commands to the queue.

The more you have running at the time the longer the wait for something that is to run right now before it actually gets to run. There have to be a huge number of things going on before the delay becomes long enough for you to notice.

The only exception to new processes getting added to the end of the queue are a special category of tasks called interrupts. Those are generally sent by separate devices where there is something that needs to be copied into memory right now or it will be lost and so the instruction to do the copy gets to jump the queue. You get to see a list of devices in your computer that are connected to interrupts when you first turn the computer on.

Basically there is no difference between running a command immediately in JavaScript and running it when the timeout finishes counting down. In both cases the command is added to the queue and runs when everything in front of it has run.

Where this delay can make a difference is where you are trying to time a process using timeouts where the commands to be run start another countdown to run the same thing again. Then you have count down to zero followed by time on the queue waiting to run followed by countdown to zero followed by etc..... So each time around takes slightly longer than the countdown would indicate. One way around that is to use setInterval instead of setTimeout because setInterval never stops counting down, it restarts automatically as soon as it reaches zero. So if you were going to use one of these commands to set up an analog clock then using setTimeout would result in the clock gradually falling further behind in when it is supposed to move the hands whereas setInterval would stay roughly the same amount behind all the time.
I don't understand what is that input queue? and what is the diffrence between command and instruction and how it connected to statements and function in Javascript? OS don't use processes and threads to manage all the programs in the computer? What is the connection between the processes and threads theory of OS and the input queue? and please where can I find more information in the web about that subject of the input queue because I didn't manage to find anything about that. I am pretty confused with all that stuff If you can please help me understand all of this. Thank you.