Sometimes the end is not very good, but at least it is a kind of ending, this series of the last article settimeout, this is a confusing function, but also I have been in the slot JS
Reason, we do not see the source code JS, Setimeout also, from the beginning to the end is the use of black boxes.
A: settimeout single-threaded query?
All the textbooks are saying that JS is a single-threaded model, and that settimeout's execution function is dropped to JS's internal execution queue, which also includes the Onlick event and some XHR callback functions.
At first glance seems to be such a matter, since to queue up, that must be the principle of FIFO, who can not guarantee accurate timing trigger, even if the exact trigger, there is no guarantee in the execution queue
Execute immediately, because to queue, if I set the 5s trigger, so the time must be above 5s, the problem is here, this 5s trigger mechanism is what??? Who can tell me
???? since JS is single-threaded, is JS going to keep rotation "execution queue"? Q Execute function 5s is it time? 5s is it time??? I don't think JS would be so stupid.
Do this "spin" operation, because if I set the trigger time to 1 years? Does it have to spin for 1 years??? And this pull mode is quite CPU-intensive, in order to save as much as possible
CPU time, will there be other threads to assist Setttimeout to do this 5s mechanism? Then 5s time to push the execution function in Setimeout into JS's internal execution queue?
What kind of a reasonable way would it be?
Second: Find inspiration in System.Threading.Timer
Because Setimeout closed source, I do not see settimeout inside exactly how to do 5s trigger mechanism, very sorry, I can only find similar language in the timer mechanism, fortunately in C #
Also has such a timer, see if can find some inspiration, then I will probably read the following source:
1 Static void Main (string[] args)2 {3 new""0 1//30s4 5 Console.read (); 6 }
Through the dazzling search, finally understand that the original timer is only a Win32 in the Createappdomaintimer function of the package, really their pit goods, first let you see the real.
One of the duetime is the period parameter in my timer constructor, which is 30s, and then periodically triggers the Appdomaintimercallback function, a logical call
The fire method triggers our callcallback function.
In fact, we see the source code, found that the timer timer is actually a fake, just encapsulated the WIN32 function, and also did not do completely 30s, this is because callback () is in the call
After the Appdomaintimercallback function is triggered, these logic also takes time, including the error of the Win32 callback, what is the inspiration now? since the multithreading of C # uses
The work thread goes to run all has the error, that your single-threaded settimeout and what can? Let's not talk about using the main thread to rotation settimeout this very unrealistic thing.
Three: The last bit of speculation
Through some understanding of the principle of the timer in C #, I think settimeout should be like this, the 5s mechanism should be dropped to the browser kernel thread, by the browser kernel thread to the real
now this 5s mechanism, if the 5s time, the kernel will plug function functions to JS "Internal execution queue", by the JS main thread idle time to execute. after all, the browser thread still has a lot
, such as the following IE9:
Well, not to mention, said more is also speculation, the outcome is not perfect, thank you for the continued attention to the JavaScript series, but also wish you all in the new year work smoothly ~ ~ ~
JavaScript Tour-terminal: the confused settimeout