問題描述
由于 JavaScript 在單線程中運行,在發(fā)出 AJAX 請求后,后臺實際發(fā)生了什么?我想對此有更深入的了解,有人能解釋一下嗎?
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 線程執(zhí)行完成時,它都會檢查隊列中是否有另一個事件要處理.如果有,它會將其從隊列中拉出并觸發(fā)該事件(例如鼠標(biāo)單擊).
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 調(diào)用下的本機代碼網(wǎng)絡(luò)將知道 ajax 響應(yīng)何時完成,并且事件將被添加到 javascript 事件隊列中.本機代碼如何知道 ajax 調(diào)用何時完成取決于實現(xiàn).它可以用線程實現(xiàn),也可以由事件驅(qū)動本身(這并不重要).實現(xiàn)的重點是,當(dāng)ajax響應(yīng)完成后,一些native代碼會知道它已經(jīng)完成,并將一個事件放入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.
如果當(dāng)時沒有 Javascript 正在運行,則將立即觸發(fā)該事件,該事件將運行 ajax 響應(yīng)處理程序.如果當(dāng)時正在運行某些東西,那么當(dāng)當(dāng)前執(zhí)行的 javascript 線程完成時,將處理該事件.javascript引擎不需要進行任何輪詢.當(dāng)一段 Javascript 完成執(zhí)行時,JS 引擎只是檢查事件隊列以查看是否還有其他需要運行的內(nèi)容.如果是這樣,它會從隊列中彈出下一個事件并執(zhí)行它(調(diào)用為該事件注冊的一個或多個回調(diào)函數(shù)).如果事件隊列中沒有任何內(nèi)容,則 JS 解釋器有空閑時間(垃圾收集或空閑),直到某個外部代理將其他內(nèi)容放入事件隊列并再次喚醒它.
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.
因為所有外部事件都經(jīng)過事件隊列,并且在 javascript 實際運行其他東西時不會觸發(fā)任何事件,所以它保持單線程.
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.
這里有一些關(guān)于細節(jié)的文章:
Here are some articles on the details:
- Javascript 定時器如何工作 - 由 John Resig 編寫
- 事件和時序深度
- W3 規(guī)范:HTML5 事件循環(huán)
- 關(guān)于事件循環(huán)的 MDN 文章
- JS 事件隊列演示
- JavaScript 事件循環(huán):解釋
- 幫助馴服異步 Javascript 的五種模式
- Javascript 事件循環(huán)演示
- 視頻討論 Javascript 的工作原理(包括 10:27 的事件循環(huán))李>
這篇關(guān)于JavaScript 如何在后臺處理 AJAX 響應(yīng)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!