Home >Web Front-end >JS Tutorial >How to use event loop

How to use event loop

php中世界最好的语言
php中世界最好的语言Original
2018-03-19 14:34:251879browse

This time I will show you how to use event loop, what are the precautions when using event loop, the following is a practical case, let's take a look.

The event loop thing really gave me a hard time. When I was asked this question during the interview, I was confused and didn’t know how to answer it at all. But when I came back and found out this When the name that sounds so mysterious and powerful refers to the knowledge of asynchronous and single-threading, my mood becomes very complicated again. It's my fault that I didn't know it had such an advanced name.

In fact, this thing is quite simple. First of all, js is a single-threaded language. The so-called single-thread can be explained clearly in one sentence--"js cannot do two things at the same time. It can only Do it one by one in order."

This sequence is relatively easy to understand - "first synchronous, then asynchronous". When js executes the code, it will be executed from top to bottom. If it is found that you are a synchronous event, it will be executed immediately ; If you find that it is an asynchronous event, I'm sorry, no matter whether you delay it for 0 seconds or 10 seconds, as long as it is an asynchronous event in nature, it will be placed in the event queue to wait. After the synchronization event processing in the execution stack (that is, the thing that stores all the synchronization events in the main thread) is completed, the main thread begins to continuously go to the event queue to query whether Events that can be pulled over for execution. (Note: When the synchronization event has just finished processing, if there are delay functions in the asynchronous queue, they will start timing at the same time ).

 The process of this main threadloopcontinuously fetching events from the event queue is called an event loop.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

Basic knowledge of html in the front-end

Using flex layout
Well-known website front-end layout analysis

The above is the detailed content of How to use event loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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