Since JavaScript runs in a single thread, after an AJAX request is made, what actually happens in the background? I would like to get a deeper insight into this, can anyone shed some light?
由于JavaScript在单个线程中运行,在发出AJAX请求后,后台实际发生了什么?我想更深入地了解这一点,任何人都能解释一下吗?
2 个解决方案
#1
199
Below the covers, javascript has an event queue. Each time a javascript thread of execution finishes, it checks to see if there is another event in the queue to process. If there is, it pulls it off the queue and triggers that event (like a mouse click, for example).
在封面下,javascript有一个事件队列。每次javascript执行线程完成时,它都会检查队列中是否还有另一个要处理的事件。如果存在,则将其从队列中拉出并触发该事件(例如,鼠标单击)。
The native code networking that lies under the ajax call will know when the ajax response is done and an event will get added to the javascript event queue. How the native code knows when the ajax call is done depends upon the implementation. It may be implemented with threads or it may also be event driven itself (it doesn't really matter). The point of the implementation is that when the ajax response is done, some native code will know it's done and put an event into the JS queue.
位于ajax调用下的本机代码网络将知道何时完成ajax响应并且将事件添加到javascript事件队列中。本机代码如何知道ajax调用何时完成取决于实现。它可以用线程实现,也可以是事件驱动本身(它并不重要)。实现的重点是,当ajax响应完成时,一些本机代码将知道它已完成并将事件放入JS队列。
If no Javascript is running at the time, the event will be immediately triggered which will run the ajax response handler. If something is running at the time, then the event will get processed when the current javascript thread of execution finishes. There doesn't need to be any polling by the javascript engine. When a piece of Javascript finishes executing, the JS engine just checks the event queue to see if there is anything else that needs to run. If so, it pops the next event off the queue and executes it (calling one or more callback functions that are registered for that event). If nothing is in the event queue, then the JS interpreter has free time (garbage collection or idle) until some external agent puts something else in the event queue and wakes it up again.
如果当时没有运行Javascript,则会立即触发该事件,该事件将运行ajax响应处理程序。如果当时正在运行某些内容,则当前执行的javascript线程完成时将处理该事件。不需要通过javascript引擎进行任何轮询。当一段Javascript完成执行时,JS引擎只检查事件队列以查看是否还有其他需要运行的事件。如果是这样,它会从队列中弹出下一个事件并执行它(调用为该事件注册的一个或多个回调函数)。如果事件队列中没有任何内容,则JS解释器具有空闲时间(垃圾收集或空闲),直到某个外部代理将其他内容放入事件队列并再次唤醒它。
Because all outside events go through the event queue and no event is ever triggered while javascript is actually running something else, it stays single threaded.
因为所有外部事件都通过事件队列,并且当javascript实际运行其他内容时没有触发事件,所以它保持单线程。
Here are some articles on the details:
以下是一些有关细节的文章:
- How Javascript Timers Work - written by John Resig
- Javascript计时器如何工作 - 由John Resig编写
- Events and Timing in Depth
- 事件和时间深度
- W3 spec: HTML5 event loops
- W3规范:HTML5事件循环
- MDN article on Event Loop
- 关于Event Loop的MDN文章
- Presentation on JS event queue
- 介绍JS事件队列
- The JavaScript Event Loop: Explained
- JavaScript事件循环:解释
- Five Patterns to Help Tame Asynchronous Javascript
- 帮助驯服异步Javascript的五种模式
- Javascript Event Loop Presentation
- Javascript事件循环演示
- Video Discussing How Javascript Works (including event loop at 10:27)
- 视频讨论Javascript如何工作(包括事件循环在10:27)
#2
15
You can find here a very complete documentation on events handling in javascript.
It is written by a guy working on the javascript implementation in the Opera Browser.
你可以在这里找到关于javascript事件处理的非常完整的文档。它是由一个致力于Opera浏览器中的javascript实现的人编写的。
More precisely, look at the titles: "Event Flow", "Event Queuing" and "Non-user Events": you'll learn that:
更准确地说,看看标题:“事件流”,“事件排队”和“非用户事件”:您将了解到:
- Javascript runs in a single thread for each browser tab or window.
- Javascript在每个浏览器选项卡或窗口的单个线程中运行。
- Events are queued and executed sequentially.
- 事件按顺序排队并执行。
- XMLHttpRequest are run by the implementation and callbacks are run using the event queue.
- XMLHttpRequest由实现运行,回调使用事件队列运行。
Note: Original link was: link, but is now dead.
注意:原始链接是:链接,但现在已经死了。
#1
199
Below the covers, javascript has an event queue. Each time a javascript thread of execution finishes, it checks to see if there is another event in the queue to process. If there is, it pulls it off the queue and triggers that event (like a mouse click, for example).
在封面下,javascript有一个事件队列。每次javascript执行线程完成时,它都会检查队列中是否还有另一个要处理的事件。如果存在,则将其从队列中拉出并触发该事件(例如,鼠标单击)。
The native code networking that lies under the ajax call will know when the ajax response is done and an event will get added to the javascript event queue. How the native code knows when the ajax call is done depends upon the implementation. It may be implemented with threads or it may also be event driven itself (it doesn't really matter). The point of the implementation is that when the ajax response is done, some native code will know it's done and put an event into the JS queue.
位于ajax调用下的本机代码网络将知道何时完成ajax响应并且将事件添加到javascript事件队列中。本机代码如何知道ajax调用何时完成取决于实现。它可以用线程实现,也可以是事件驱动本身(它并不重要)。实现的重点是,当ajax响应完成时,一些本机代码将知道它已完成并将事件放入JS队列。
If no Javascript is running at the time, the event will be immediately triggered which will run the ajax response handler. If something is running at the time, then the event will get processed when the current javascript thread of execution finishes. There doesn't need to be any polling by the javascript engine. When a piece of Javascript finishes executing, the JS engine just checks the event queue to see if there is anything else that needs to run. If so, it pops the next event off the queue and executes it (calling one or more callback functions that are registered for that event). If nothing is in the event queue, then the JS interpreter has free time (garbage collection or idle) until some external agent puts something else in the event queue and wakes it up again.
如果当时没有运行Javascript,则会立即触发该事件,该事件将运行ajax响应处理程序。如果当时正在运行某些内容,则当前执行的javascript线程完成时将处理该事件。不需要通过javascript引擎进行任何轮询。当一段Javascript完成执行时,JS引擎只检查事件队列以查看是否还有其他需要运行的事件。如果是这样,它会从队列中弹出下一个事件并执行它(调用为该事件注册的一个或多个回调函数)。如果事件队列中没有任何内容,则JS解释器具有空闲时间(垃圾收集或空闲),直到某个外部代理将其他内容放入事件队列并再次唤醒它。
Because all outside events go through the event queue and no event is ever triggered while javascript is actually running something else, it stays single threaded.
因为所有外部事件都通过事件队列,并且当javascript实际运行其他内容时没有触发事件,所以它保持单线程。
Here are some articles on the details:
以下是一些有关细节的文章:
- How Javascript Timers Work - written by John Resig
- Javascript计时器如何工作 - 由John Resig编写
- Events and Timing in Depth
- 事件和时间深度
- W3 spec: HTML5 event loops
- W3规范:HTML5事件循环
- MDN article on Event Loop
- 关于Event Loop的MDN文章
- Presentation on JS event queue
- 介绍JS事件队列
- The JavaScript Event Loop: Explained
- JavaScript事件循环:解释
- Five Patterns to Help Tame Asynchronous Javascript
- 帮助驯服异步Javascript的五种模式
- Javascript Event Loop Presentation
- Javascript事件循环演示
- Video Discussing How Javascript Works (including event loop at 10:27)
- 视频讨论Javascript如何工作(包括事件循环在10:27)
#2
15
You can find here a very complete documentation on events handling in javascript.
It is written by a guy working on the javascript implementation in the Opera Browser.
你可以在这里找到关于javascript事件处理的非常完整的文档。它是由一个致力于Opera浏览器中的javascript实现的人编写的。
More precisely, look at the titles: "Event Flow", "Event Queuing" and "Non-user Events": you'll learn that:
更准确地说,看看标题:“事件流”,“事件排队”和“非用户事件”:您将了解到:
- Javascript runs in a single thread for each browser tab or window.
- Javascript在每个浏览器选项卡或窗口的单个线程中运行。
- Events are queued and executed sequentially.
- 事件按顺序排队并执行。
- XMLHttpRequest are run by the implementation and callbacks are run using the event queue.
- XMLHttpRequest由实现运行,回调使用事件队列运行。
Note: Original link was: link, but is now dead.
注意:原始链接是:链接,但现在已经死了。