JavaScript event-driven mechanism & amp; timer mechanism, javascript event mechanism

Source: Internet
Author: User

JavaScript event-driven mechanism & timer mechanism, and javascript event mechanism

In browsers, Events Act as an extremely important mechanism to provide JavaScript with the ability to respond to user operations and DOM changes. In NodeJS, asynchronous event-driven models increase concurrency.

Basic.

I. How the program responds to the Event program to respond to external events in two ways:
1. interrupted

The operating system processes keyboard and other hardware input through interruption. The advantage of this method is that even if there is no multithreading, we can safely execute our code. After the CPU receives the interruption signal

The corresponding interrupt processing program is automatically executed. After the process is completed, the execution environment of the original code will be resumed. This method requires hardware support and is generally encapsulated by the operating system.

.

2. check whether there are any events in the round robin process. If yes, execute the corresponding processing program. This applies to both underlying and upper-layer development.

One disadvantage of the polling method is that if time-consuming operations are performed in the message loop of the main thread, the program will not be able to respond to new messages in a timely manner.

Ii. Features of the timer function in JavaScript

Both Node and browser have the setTimeout and setInterval timer functions, which have the same features.

The timer in JavaScript is not the same as the Scheduled interrupt at the bottom of the computer. When the interruption arrives, the current Execution Code is interrupted and switched to the scheduled interrupt processing function. While the JavaScript timer goes

If no code is being executed by the current execution thread, the corresponding callback function is executed. If the current code is being executed, 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 is processed after the current code is executed.

Var time1 = new Date (). getTime (); // start time point setTimeout (function () {time2 = new Date (). getTime (); console. timeEnd (time2-time1); // end time}, 100); for (var I = 0; I <100000; I ++ ){}

Run the above Code and you can see that the final output time is not about ms, but several seconds. This indicates that the scheduled callback function is not executed before the loop is completed, but is postponed to the loop.

End. In fact, in JavaScript code execution, all events cannot be processed. You must wait until all the current code is complete before you can handle new events. This is why

When the line consumes JavaScript code, the browser will lose the response.

Iii. How the timer works

1. the javascript engine has only one thread, forcing asynchronous events to be added to the queue for execution.

2. when executing asynchronous code, if the timer is blocked by the code being executed, it will enter the end of the queue to wait for execution until the next possible execution time (may exceed the set time)

). SetTimeout and setInterval are essentially different: the setTimeout Code requires a delay of "specified delay in milliseconds" at least after each callback function execution.

Rows (may be more, but not less ). However, setInterval tries to execute a callback function every other time, regardless of whether the previous callback function is still being executed.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.