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