In the browser, events act as an extremely important mechanism to give JavaScript the ability to respond to user actions and Dom changes; in Nodejs, the asynchronous event-driven model is the one that improves concurrency.
Basis.
first, how the program responds to eventsThere are two ways in which programs respond to external events:
1. Interrupts
The operating system handles hardware input such as keyboards through interrupts, and the advantage is that even without multithreading, we can safely execute our code after the CPU receives the interrupt signal
Automatically goes to execute the corresponding interrupt handler, which resumes the execution environment of the original code after processing is completed. This approach requires hardware support, which is typically encapsulated by the operating system
To.
2. PollingThe loop detects if an event occurs, and if so, executes the appropriate handler. This is used in the development of the bottom and upper layers.
One drawback of polling is that the program cannot respond to new messages in a timely manner if the time-consuming operation is performed in the message loop of the main thread.
second, the characteristics of the timer function in JavaScript
Both node and browser have settimeout and setinterval timer functions, and their working characteristics are basically the same.
The timers in JavaScript are different from the timer interrupts at the bottom of the computer. When the interrupt arrives, the current execution code is interrupted to execute the timed interrupt handler. and the JavaScript timer to
Executes the appropriate callback function if the current execution thread does not have the code being executed, or if the current code is executing, the JavaScript engine will not interrupt the current code to execute the callback.
It does not open a new thread to execute the callback, but does not process the current code until it finishes executing.
var time1 = new Date (). GetTime ();//Start time point settimeout (function () {time2 = new date (). GetTime (); Console.timeend (time2-time1);//End point}, +); for (var i = 0; i < 100000; i++) {}
Executing the above code, you can see that the final output time is not about 100ms, but a few seconds. This means that the timing callback function is not actually executed until the loop is complete, but is deferred to the loop
End. In fact, in JavaScript code execution, all events cannot be processed and must wait until the current code is complete before the new event can be processed. This is why it is shipped in the browser
When the line is time consuming JavaScript code, the browser loses its response.
Three, the operation principle of the timer
1. The JavaScript engine has only one thread, forcing an asynchronous event to join the queue for execution only.
2. When executing asynchronous code, if the timer is blocked by the code being executed, it will go to the end of the queue to wait until the next possible time to execute (may exceed the set
Delay time). SetTimeout and SetInterval are fundamentally different: SetTimeout This code will require at least a delay of "specify the millisecond value of latency" after each callback function executes
Line (may be more, but not less). However, SetInterval will try to execute the callback function once every "specified delay millisecond value", regardless of whether the previous callback function is still executing.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JavaScript event-driven mechanism & timer mechanism