JavaScript如何在后台处理AJAX响应? [英] How does JavaScript handle AJAX responses in the background?

查看:175
本文介绍了JavaScript如何在后台处理AJAX响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于JavaScript在单个线程中运行,在发出AJAX请求后,后台实际发生了什么?我想更深入地了解这一点,任何人都能解释一下吗?

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有一个事件队列。每次javascript执行线程完成时,它都会检查队列中是否还有另一个要处理的事件。如果有,它会将其从队列中拉出并触发该事件(例如鼠标点击)。

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).

ajax调用下的本机代码网络将知道何时完成ajax响应并将事件添加到javascript事件队列中。本机代码如何知道ajax调用何时完成取决于实现。它可以用线程实现,也可以是事件驱动本身(它并不重要)。实现的重点是,当完成ajax响应时,一些本机代码将知道它已完成并将事件放入JS队列。

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.

如果当时没有运行Javascript,将立即触发事件,该事件将运行ajax响应处理程序。如果当时正在运行某些内容,则当前执行的javascript线程结束时将处理该事件。不需要通过javascript引擎进行任何轮询。当一段Javascript完成执行时,JS引擎只检查事件队列以查看是否还有其他需要运行的事件。如果是这样,它会从队列中弹出下一个事件并执行它(调用为该事件注册的一个或多个回调函数)。如果事件队列中没有任何内容,则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实际运行其他内容时没有触发事件,所以它保持单线程。

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.

这里有一些文章详情:

  • How Javascript Timers Work - written by John Resig
  • Events and Timing in Depth
  • W3 spec: HTML5 event loops
  • MDN article on Event Loop
  • Presentation on JS event queue
  • The JavaScript Event Loop: Explained
  • Five Patterns to Help Tame Asynchronous Javascript
  • Javascript Event Loop Presentation
  • Video Discussing How Javascript Works (including event loop at 10:27)

这篇关于JavaScript如何在后台处理AJAX响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆