Home  >  Article  >  Web Front-end  >  (Common interview questions) js basics from setTimeout to asynchronous principle of js

(Common interview questions) js basics from setTimeout to asynchronous principle of js

php是最好的语言
php是最好的语言Original
2018-08-03 09:48:022036browse

These are some very basic but easily overlooked issues. Everyone knows how to use this method but has not studied in depth what the process is. Without further ado, look at the code below

 <input type="text" value="a" name="input" onkeydown="alert(this.value)" />
<input type="text" value="a" name="input" onkeydown="var me=this;setTimeout(function(){alert(me.value)},0)" >

When one keydown is performed, the original value in the input pops up, but when the second keydown is performed, the updated value pops up. This is because setTimeout, although its delay is set to 0, is triggered almost immediately. But it is still added to the back of the execution queue, but for this asynchronous process, the rendering has been completed. When the callback function is executed, the output is already the updated value. Another problem here is that this indicates different objects inside and outside different functions. When there is a function in the function, pay more attention to this. It is easy to make mistakes. Just be careful and don't say much.

Next, js js is single-threaded. It is conceivable that if there is no multi-threading, the entire program will be stuck. Fortunately, the browser is multi-threaded, and the browser makes js asynchronous. Some attributes of js: js is a single-threaded language. The browser only assigns one main thread to js to execute tasks (functions), but only one task can be executed at a time. These tasks form a task queue waiting to be executed, but certain tasks on the front end Some tasks are very time-consuming, such as network requests, timers and event monitoring. If they are queued up to wait for execution like other tasks, the execution efficiency will be very low and even cause the page to freeze. Therefore, the browser has opened up additional threads for these time-consuming tasks, mainly including http request threads, browser timing triggers, and browser event trigger threads. These tasks are asynchronous.

The browser opens a separate thread for asynchronous tasks such as network requests. So the question is, how does the main thread know after these asynchronous tasks are completed? The answer is the callback function. The entire program is event-driven. Each event will be bound to the corresponding callback function. For example, there is a piece of code that sets a timer.

setTimeout(function(){
    console.log(time is out);
},500);

When this code is executed, browse The server performs timing operations asynchronously. When 500ms arrives, a timing event will be triggered. At this time, the callback function will be placed in the task queue. The entire program is driven by such events.
So, js has always been single-threaded, and the browser is the key to achieving asynchronous

The following is transferred from the Internet:

js has been doing a job, which is to retrieve data from the task queue Extract the task and put it into the main thread for execution. Let’s have a deeper understanding below.
event loop
The picture comes from Philip Roberts' speech "Help, I'm stuck in an event-loop" is very profound!
Let’s map the concept we just learned to the picture. The threads that the browser opens for asynchronous tasks mentioned above can be collectively understood as WebAPIs. The task queue mentioned above is the callback queue. What we call The main thread is the part composed of dotted lines. The heap and stack together form the js main thread. The execution of functions is achieved by pushing and popping the stack. For example, there is a foo() function in the picture. The main thread pushes it into the stack. When executing the function body, it finds that the above functions still need to be executed, so it pushes these functions into the stack. When the function is executed, the function is popped off the stack. When the stack is cleared, it means that a task has been executed. At this time, the next task will be searched from the callback queue and pushed into the stack (this search process is called event loop, because it always loops to find whether there is a task in the task queue. There are also tasks).

Related articles:

Detailed explanation of js asynchronous file loader, detailed explanation of js asynchronous loading

How does native JS implement asynchronous requests to implement Ajax

Related videos:

JS#jQuery width and height understanding and application

The above is the detailed content of (Common interview questions) js basics from setTimeout to asynchronous principle of js. 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