JavaScript event-driven mechanism & timer mechanism

Source: Internet
Author: User

in the browser, events act as an extremely important mechanism to give JavaScript the ability to respond to user actions and DOM changes. In the Nodejs. The asynchronous event-driven model is the basis for improving concurrency capabilities .

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. The advantage of this approach is that even if there is no multi-threading, we can safely run our code, the CPU received an interrupt signal after its own initiative to run the corresponding interrupt handler, after processing will restore the original code of the running environment continue to run.

Such a way requires hardware support. Generally, they are encapsulated by the operating system .

2. PollingThe cyclic detection of whether there is an event, the fake has to go to run the corresponding processing program. This is used in the development of the bottom and upper layers.

One drawback of polling is that it assumes a time-consuming operation in the main thread's message loop. The program will not be able to respond to new messages in a timely manner.

second, the characteristics of the timer function in JavaScript

Both the settimeout and the SetInterval timer functions are available in either node or in the browser. and its 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 running code is interrupted. Go to run the timer interrupt handler function.

While the JavaScript timer comes in , assume that the current running thread does not have code running. The corresponding callback function is run, assuming that the current code is running, the JavaScript engine does not interrupt the current code to run the callback, and does not open a new thread to run the callback, but only after the current code has finished running.

Console.time ("Settimoutlabel");//Mark time starts with settimeout (function () {console.timeend ("Settimoutlabel");//Mark Time end}, 100); for (var i = 0; i < 100000; i++) {}

Run the above code. It is possible to see that the output time is not about 100ms, but a few seconds.

This means that the timer callback function is not actually running until the loop is complete. Instead, it was postponed to the end of the cycle . In fact, all of the events in the JavaScript code are not processed and must wait until the current code is complete, to handle the new events. That's why the browser loses its response when it's time consuming JavaScript code in the browser.

Three, the operation principle of the timer

1. The JavaScript engine only has one thread, forcing an asynchronous event to simply increase the queue to wait for it to run.

2. When running asynchronous code, assuming that the timer is blocked by the running code, it will go to the end of the queue to wait until the next possible time to run (possibly exceeding the set delay time). SetTimeout and SetInterval are fundamentally different: SetTimeout This code will require at least a delay of "specifying the delay millisecond value" after each callback function is run (there may be many others, but not less). However, the setinterval will attempt to run the callback function once every time the delay millisecond value is specified. Whether or not the previous callback function is still running.


JavaScript event-driven mechanism &amp; timer mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.