Home Web Front-end JS Tutorial Detailed explanation of the characteristics of JS single-threaded asynchronous io callback

Detailed explanation of the characteristics of JS single-threaded asynchronous io callback

Jan 03, 2018 am 10:46 AM
javascript characteristic Detailed explanation

Most of our initial contact with JavaScript started with js scripts in HTML. However, we have been using this seemingly simple language for several years, and we have not yet understood some of its principles and mechanisms. Are there any mistakes? At least JavaScript uses various event callbacks when operating DOM, such as button, link click, mouse passing, focus acquisition, etc. This article mainly analyzes the characteristics of JavaScript single-threaded asynchronous io callbacks. I hope that the content we have compiled can Help you.

In this process, we bind an event callback function on the dom such as onclick="doCheck()" This process is to register a click event for the dom element and bind an event callback function doCheck ().

When the mouse clicks on this element, an event is triggered, and the event binding function is immediately executed and returned.

Later, when I came into contact with jquery, a large number of


$("#id").click(function(){
alert('点击事件');
});
Copy after login

I seem to be used to this jquery syntax as I write more and more, but you have noticed that the previous selector only selects and filters the dom node, and the subsequent click is a Event registration, and the function(){} inside is actually a parameter, the parameter of the event binding function, which requires you to be familiar with the syntax of javascript.

function is an object in javascript, and the object is It can refer to everything in the world, so objects can contain many attributes, methods, etc.

Since it is an object, it can be passed as a parameter. This kind of function is called a higher-order function.

And this kind of function doesn’t have a defined name, right? Of course you can give it a name, and it’s the same thing if you pass the name over, but it’s meaningless because the function object here is actually a formal parameter, so we are used to not giving this kind of function a name. Name, this is what is often called an anonymous function.

Following the $("#id").click above, when the click event is triggered, the event binding function must be executed. Directly with the above The onclick method given on the DOM has the same effect.

Assume that there are multiple threads in the browser to operate the script, can you imagine the chaos? Thread 1 is about to modify the value of element A , but I didn’t expect that thread two had already deleted element A from the DOM tree. At this time, thread one failed to operate and reported an error. This situation is not terrible. Either the browser fails to maintain the consistency of data across multiple threads, or the front-end engineer Maintain it yourself, so... the browser only has one thread to operate the dom, which saves a lot of unnecessary trouble.


setTimeout(function(){
alert('弹出');
},300);

while(true){
	........
}
Copy after login

Do you think 300 milliseconds Is there any drama after alert('pop-up')?

No, there will never be drama. Waiting for 300 milliseconds is just to deceive your emotions. Because the browser executes the script in single-thread mode.

Once the thread is in infinite loop mode and executes the while statement, your setTimeout will no longer have any effect.

Then we enter the node.js world, which completely retains the characteristics of javascript in the browser , single-threaded asynchronous callback, it is precisely because of this feature that it is what it is. If node.js is a synchronous language, even if all npm packages are extended by c++ (the speed is fast enough), no matter how fast you are, you can't beat c. Language processing speed, then node.js may have been despised by PHP before it was born.

It is precisely because of its asynchronous callback IO that it can improve its efficiency, which reminds me of a student from my previous school. A comparison between a fast food restaurant and a school cafeteria:

In the cafeteria, all the students lined up in a line at the window with their plates. The sister who prepared the meal filled it one by one, and then left with the rice one by one. This is It is the result of synchronous processing.

School fast food restaurants also have students queuing up to order, but after ordering, you can take your pager and leave to find a seat. In this way, the waiter can provide services to many people within a unit time. Moreover, students who have ordered a meal can find a seat to do other things on their own, instead of standing stupidly at the window waiting for the meal. The moment your meal comes out, the server will press the code according to the order number, and then the call on your table will The caller will ring, and you can just go and get the meal. This is asynchronous processing. When the caller sounds, it triggers an event.

Single thread can reduce resource waste and maintenance difficulties caused by status switching between multiple threads. Of course, There are also specialized third-party packages to support multi-core and multi-thread scenarios, you can weigh it yourself.

Related recommendations:

Single-threaded setTimeout in JavaScript

How the single-threaded programming language PHP implements multi-threaded operations

Introduces in detail some things about JavaScript single-threading (picture)

The above is the detailed content of Detailed explanation of the characteristics of JS single-threaded asynchronous io callback. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Learn more about Promise.resolve() Learn more about Promise.resolve() Feb 18, 2024 pm 07:13 PM

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

C++ function types and characteristics C++ function types and characteristics Apr 11, 2024 pm 03:30 PM

C++ functions have the following types: simple functions, const functions, static functions, and virtual functions; features include: inline functions, default parameters, reference returns, and overloaded functions. For example, the calculateArea function uses π to calculate the area of ​​a circle of a given radius and returns it as output.

See all articles