Posted 鍓嶇椋熷爞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

瑙傛劅搴︼細馃専馃専馃専馃専馃専


鍙e懗锛氱敇姊呭湴鐡?/p>


鐑归オ鏃堕棿锛?0min




姣忓ぉ閮藉湪鍐橨avaScript鐨勪綘锛屾槸鍚︽竻妤欽avaScript寮曟搸鐨勫師鐞嗗憿锛?/p>


鎯宠浜嗚ВJavaScript寮曟搸锛岄鍏堟垜浠粠瀹冪殑杩愯鏈哄埗Event Loop鏉ヨ璧枫€?/p>


棣栧厛绉戞櫘涓€浜涘熀纭€鐭ヨ瘑銆?/p>


杩涚▼鍜岀嚎绋?/strong>


杩涚▼



绾跨▼


绾跨▼鏄繘绋嬪唴鐨勪竴涓嫭绔嬫墽琛屽崟鍏冿紝鍦ㄤ笉鍚岀殑绾跨▼涔嬮棿鏄彲浠ュ叡浜繘绋嬭祫婧愮殑銆?/p>


鏈夊彞鑰佽瘽鏄繖鏍疯鐨勶紝绌峰吇鍎垮瓙瀵屽吇濂炽€?/p>


杩涚▼灏辨槸涓€涓瘜浜屼唬鐖哥埜锛屽畠閫夋嫨浜嗙┓鍏荤嚎绋嬪効瀛愩€?/strong>






涓€鍙ヨ瘽瑙i噴杩涚▼鍜岀嚎绋?/strong>


杩涚▼锛氳祫婧愬垎閰嶇殑鏈€灏忓崟浣?/p>


绾跨▼锛氱▼搴忔墽琛岀殑鏈€灏忓崟浣?/p>


鍏充簬杩涚▼鍜岀嚎绋嬫柟闈㈢殑鐭ヨ瘑鎴戜滑鍏堜簡瑙e埌杩欙紝鎰熷叴瓒g殑鍚屽浠彲浠ョЩ姝?/p>

https://www.cnblogs.com/Jones-dd/p/8858995.html


Q&A鍐嶆潵鍥炵瓟涓€涓棶棰橈細


鍦ㄥ绾跨▼鎿嶄綔涓嬪彲浠ュ疄鐜板簲鐢ㄧ殑骞惰澶勭悊锛屼粠鑰屼互鏇撮珮鐨?CPU 鍒╃敤鐜囨彁楂樻暣涓簲鐢ㄧ▼搴忕殑鎬ц兘鍜屽悶鍚愰噺銆傜壒鍒槸鐜板湪寰堝璇█閮芥敮鎸佸鏍稿苟琛屽鐞嗘妧鏈紝鐒惰€?JavaScript 鍗翠互鍗曠嚎绋嬫墽琛岋紝涓轰粈涔堝憿锛?/p>


绛旓細JavaScript浣滀负鑴氭湰璇█锛屾渶鍒濊璁捐鐢ㄤ簬娴忚鍣ㄣ€備负浜嗛伩鍏嶅鏉傜殑鍚屾闂(鍋氫汉鍢涳紝杩樻槸绠€鍗曠偣濂斤紝璇█涔熶竴鏍?锛屽鏋淛avaScript鍚屾椂鏈変袱涓嚎绋嬶紝涓€涓嚎绋嬩腑鎵ц鍦ㄦ煇涓狣OM鑺傜偣涓婃坊鍔犲唴瀹癸紝鍙︿竴涓嚎绋嬫墽琛屽垹闄よ繖涓妭鐐癸紝杩欐椂娴忚鍣ㄤ細鈥︹€?/p>


濡備綍瑙i噴Event Loop闈㈣瘯瀹樻墠婊℃剰锛?></p> 
<p><br></p> 
<p>鎵€浠avaScript鐨勫崟绾跨▼鏄繖闂ㄨ瑷€鐨勬牳蹇冿紝鏈潵涔熶笉浼氭敼鍙樸€?nbsp;</p> 
<p><br></p> 
<p>鏈変汉璇达紝閭TML5鐨勬柊鐗规€eb Worker锛屽彲浠ュ垱寤哄绾跨▼鍛€锝?/p> 
<p><br></p> 
<p>鏄殑锛屼负浜嗚В鍐充笉鍙伩鍏嶇殑鑰楁椂鎿嶄綔(澶氶噸寰幆銆佸鏉傜殑杩愮畻)锛孒TML5鎻愬嚭浜哤eb Worker锛屽畠浼氬湪褰撳墠鐨刯s鎵ц涓荤嚎绋嬩腑寮€杈熷嚭涓€涓澶栫殑绾跨▼鏉ヨ繍琛宩s鏂囦欢锛岃繖涓柊鐨勭嚎绋嬪拰js涓荤嚎绋嬩箣闂翠笉浼氫簰鐩稿奖鍝嶏紝鍚屾椂鎻愪緵浜嗘暟鎹氦鎹㈢殑鎺ュ彛锛歱ostMessage鍜宱nMessage銆?/p> 
<p><br></p> 
<p>浣嗘槸鍥犱负瀹冨垱寤虹殑瀛愮嚎绋嬪畬鍏ㄥ彈鎺т簬涓荤嚎绋嬶紝涓斾綅浜庡閮ㄦ枃浠朵腑锛屾棤娉曡闂瓺OM銆傛墍浠ュ畠骞舵病鏈夋敼鍙榡s鍗曠嚎绋嬬殑鏈川銆?/p> 
<p><br></p> 
<p>鍗曠嚎绋嬪氨鎰忓懗鐫€锛屾墍鏈夌殑浠诲姟閮介渶瑕佹帓闃熴€?/p> 
<p><br></p> 
<p>灏卞儚杩樹笉鑳借嚜鍔╃偣椁愮殑鏃跺€欎綘鍘昏偗寰峰熀闇€瑕佹帓闃燂紝鏈夌殑浜烘病鎯冲ソ鐐逛粈涔堟垨鑰呯偣鐨勪笢瑗垮緢澶氾紝鑰楁椂灏变細闀匡紝閭d箞鍚庨潰鐨勪汉涔熷彧濂芥帓闃熺瓑寰呫€傛湁浜嗚嚜鍔╃偣椁愭湇鍔″悗锛屼竴鍒囬棶棰樿繋鍒冭€岃В銆?/p> 
<p><br></p> 
<p class=濡備綍瑙i噴Event Loop闈㈣瘯瀹樻墠婊℃剰锛?></p> 
<p><br></p> 
<p>璇█鐨勮璁″拰鐢熸椿涓殑鐜板疄鎯呭喌寰堝儚锛孖O璁惧(杈撳叆杈撳嚭)寰堟參(姣斿Ajax)锛岄偅涔堣瑷€鐨勮璁¤€呮剰璇嗗埌杩欎竴鐐癸紝灏卞湪涓荤嚎绋嬩腑鎸傝捣澶勪簬绛夊緟涓殑浠诲姟锛屽厛杩愯鍚庨潰鐨勪换鍔★紝绛塈O璁惧鏈変簡缁撴灉锛屽啀鎶婃寕璧风殑浠诲姟鎵ц涓嬪幓銆?/p> 
<p><br></p> 
<p><span class=Event Loop 


濡備綍瑙i噴Event Loop闈㈣瘯瀹樻墠婊℃剰锛?></p> 
<p><span class=

浠庝笂鍥句腑鎴戜滑鍙互鐪嬪埌锛屽湪涓荤嚎绋嬭繍琛屾椂锛屼細浜х敓鍫?heap)鍜屾爤(stack)銆?/p>


鍫嗕腑瀛樼殑鏄垜浠0鏄庣殑object绫诲瀷鐨勬暟鎹紝鏍堜腑瀛樼殑鏄熀鏈暟鎹被鍨嬩互鍙婂嚱鏁版墽琛屾椂鐨勮繍琛岀┖闂淬€?/p>


鏍堜腑鐨勪唬鐮佷細璋冪敤鍚勭澶栭儴API锛屽畠浠湪浠诲姟闃熷垪涓姞鍏ュ悇绉嶄簨浠?onClick,onLoad,onDone)锛屽彧瑕佹爤涓殑浠g爜鎵ц瀹屾瘯(js寮曟搸瀛樺湪monitoring process杩涚▼锛屼細鎸佺画涓嶆柇鐨勬鏌ヤ富绾跨▼鎵ц鏍堟槸鍚︿负绌?锛屼富绾跨▼灏卞洖鍘昏鍙栦换鍔¢槦鍒楋紝鍦ㄦ寜椤哄簭鎵ц杩欎簺浜嬩欢瀵瑰簲鐨勫洖璋冨嚱鏁般€?/p>


涔熷氨鏄涓荤嚎绋嬩粠浠诲姟闃熷垪涓鍙栦簨浠讹紝杩欎釜杩囩▼鏄惊鐜笉鏂殑锛屾墍浠ヨ繖绉嶈繍琛屾満鍒跺張鎴愪负Event Loop(浜嬩欢寰幆)銆?/p>


鍚屾浠诲姟鍜屽紓姝ヤ换鍔?/strong>


鎴戜滑鍙互灏嗕换鍔″垎涓哄悓姝ヤ换鍔″拰寮傛浠诲姟銆?/p>


鍚屾浠诲姟灏辨槸鍦ㄤ富绾跨▼涓婃帓闃熸墽琛岀殑浠诲姟锛屽彧鑳芥墽琛屽畬涓€涓啀鎵ц涓嬩竴涓€?/p>


寮傛浠诲姟鍒欎笉杩涘叆涓荤嚎绋嬶紝鑰屾槸鍏堝湪event table涓敞鍐屽嚱鏁帮紝褰撴弧瓒宠Е鍙戞潯浠跺悗锛屾墠鍙互杩涘叆浠诲姟闃熷垪鏉ユ墽琛屻€傚彧鏈変换鍔¢槦鍒楅€氱煡涓荤嚎绋嬭锛屾垜杩欒竟寮傛浠诲姟鍙互鎵ц浜嗭紝杩欎釜鏃跺€欐浠诲姟鎵嶄細杩涘叆涓荤嚎绋嬫墽琛屻€?/p>


涓句釜馃尠


console.log(a);
setTimeout( function () { console.log(b); },1000) console.log(c)
// a// b// c


1.console.log(a)鏄悓姝ヤ换鍔★紝杩涘叆涓荤嚎绋嬫墽琛岋紝鎵撳嵃a銆?/p>

2.setTimeout鏄紓姝ヤ换鍔★紝鍏堣鏀惧叆event table涓敞鍐岋紝1000ms涔嬪悗杩涘叆浠诲姟闃熷垪銆?/p>

3.console.log(c)鏄悓姝ヤ换鍔★紝杩涘叆涓荤嚎绋嬫墽琛岋紝鎵撳嵃c銆?/p>

褰揳锛宑琚墦鍗板悗锛屼富绾跨▼鍘讳簨浠堕槦鍒椾腑鎵惧埌setTimeout閲岀殑鍑芥暟锛屽苟鎵ц锛屾墦鍗癰銆?/p>



缁间笂鎵€杩帮紝b鏈€鎸佷箙锝?鎵釜馃)


瀹忎换鍔″拰寰换鍔?/strong>


鍚屾浠诲姟鍜屽紓姝ヤ换鍔$殑鍒掑垎鍏跺疄骞朵笉鍑嗙‘锛屽噯纭殑鍒嗙被鏂瑰紡鏄畯浠诲姟(Macrotask)鍜屽井浠诲姟(Microtask)銆?/p>


瀹忎换鍔″寘鎷細

script(鏁翠綋浠g爜), setTimeout, setInterval, setImmediate, I/O, UI rendering銆?


寰换鍔″寘鎷細

process.nextTick, Promise,

Object.observe(宸插簾寮?, MutationObserver(html5鏂扮壒鎬?銆?/p>



杩欑鍒嗙被鐨勬墽琛屾柟寮忓氨鏄紝鎵ц涓€涓畯浠诲姟锛岃繃绋嬩腑閬囧埌寰换鍔℃椂锛屽皢鍏舵斁鍒板井浠诲姟鐨勪簨浠堕槦鍒楅噷锛屽綋鍓嶅畯浠诲姟鎵ц瀹屾垚鍚庯紝浼氭煡鐪嬪井浠诲姟鐨勪簨浠堕槦鍒楋紝渚濇鎵ц閲岄潰鐨勫井浠诲姟銆傚鏋滆繕鏈夊畯浠诲姟鐨勮瘽锛屽啀閲嶆柊寮€鍚畯浠诲姟鈥︹€?/p>


濡備綍瑙i噴Event Loop闈㈣瘯瀹樻墠婊℃剰锛?></p> 
<p><br></p> 
<p>鍐嶄妇涓煂?/p> 
<p><br></p> 
<section class=

setTimeout(function() { console.log('a')});
new Promise(function(resolve) { console.log('b');
for(var i =0; i <10000; i++) { i ==99 && resolve(); }}).then(function() { console.log('c')});
console.log('d');
// b// d// c// a


1.棣栧厛鎵цscript涓嬬殑瀹忎换鍔★紝閬囧埌setTimeout锛屽皢鍏舵斁鍏ュ畯浠诲姟鐨勯槦鍒楅噷銆?/p>

2.閬囧埌Promise锛宯ew Promise鐩存帴鎵ц锛屾墦鍗癰銆?/p>

3.閬囧埌then鏂规硶锛屾槸寰换鍔★紝灏嗗叾鏀惧埌寰换鍔$殑闃熷垪閲屻€?/p>

4.閬囧埌console.log('d')锛岀洿鎺ユ墦鍗般€?/p>

5.鏈疆瀹忎换鍔℃墽琛屽畬姣曪紝鏌ョ湅寰换鍔★紝鍙戠幇then鏂规硶閲岀殑鍑芥暟锛屾墦鍗癱銆?/p>

6.鏈疆event loop鍏ㄩ儴瀹屾垚銆?/p>

7.涓嬩竴杞惊鐜紝鍏堟墽琛屽畯浠诲姟锛屽彂鐜板畯浠诲姟闃熷垪涓湁涓€涓猻etTimeout锛屾墦鍗癮銆?/p>



缁间笂鎵€杩帮紝涓嶈璇碼鏄渶鎸佷箙鐨勶紝濡傛灉浣犺涓轰綘褰诲簳鏄庣櫧浜嗭紝缁欎綘鍑洪亾棰橈紝鐪嬬湅涓嬮潰鐨勪唬鐮佷腑锛岃皝鏈€鎸佷箙锛?/p>


console.log('a');
setTimeout(function() { console.log('b'); process.nextTick(function() { console.log('c'); }) new Promise(function(resolve) { console.log('d'); resolve(); }).then(function() { console.log('e') })})process.nextTick(function() { console.log('f');})new Promise(function(resolve) { console.log('g'); resolve();}).then(function() { console.log('h')})
setTimeout(function() { console.log('i'); process.nextTick(function() { console.log('j'); }) new Promise(function(resolve) { console.log('k'); resolve(); }).then(function() { console.log('l') })})



濂斤紝涓嶈鎬傦紝鎴戜滑鏉ラ€愭鍒嗘瀽銆?/p>


绗竴杞簨浠跺惊鐜細


1.绗竴涓畯浠诲姟(鏁翠綋script)杩涘叆涓荤嚎绋嬶紝console.log('a')锛屾墦鍗癮銆?/p>

2.閬囧埌setTimeout锛屽叾鍥炶皟鍑芥暟杩涘叆瀹忎换鍔¢槦鍒楋紝鏆傚畾涔変负setTimeout1銆?/p>

3.閬囧埌process.nextTick()锛屽叾鍥炶皟鍑芥暟琚垎鍙戝埌寰换鍔¢槦鍒楋紝鏆傚畾涔変负process1銆?/p>

4.閬囧埌Promise锛宯ew Promise鐩存帴鎵ц锛屾墦鍗癵銆倀hen杩涘叆寰换鍔¢槦鍒楋紝鏆傚畾涔変负then1銆?/p>

5.閬囧埌setTimeout锛屽叾鍥炶皟鍑芥暟杩涘叆瀹忎换鍔¢槦鍒楋紝鏆傚畾涔変负setTimeout2銆?/p>



姝ゆ椂鎴戜滑鐪嬩竴涓嬩袱涓换鍔¢槦鍒椾腑鐨勬儏鍐碉細


瀹忎换鍔¢槦鍒楋細setTimeout1銆乻etTimeout2

寰换鍔¢槦鍒楋細process1銆乼hen1


绗竴杞畯浠诲姟鎵ц瀹屾瘯锛屾墦鍗板嚭a鍜実銆?/p>

鏌ユ壘寰换鍔¢槦鍒椾腑鏈塸rocess1鍜宼hen1銆傚叏閮ㄦ墽琛岋紝鎵撳嵃f鍜宧銆?/p>

绗竴杞簨浠跺惊鐜畬姣曪紝鎵撳嵃鍑篴銆乬銆乫鍜宧銆?/p>


绗簩杞簨浠跺惊鐜細


1.浠巗etTimeout1瀹忎换鍔″紑濮嬶紝棣栧厛鏄痗onsole.lob('b')锛屾墦鍗癰銆?/p>

2.閬囧埌process.nextTick()锛岃繘鍏ュ井浠诲姟闃熷垪锛屾殏瀹氫箟涓簆rocess2銆?/p>

3.new Promise鐩存帴鎵ц锛岃緭鍑篸锛宼hen杩涘叆寰换鍔¢槦鍒楋紝鏆傚畾涔変负then2銆?/p>


姝ゆ椂涓や釜浠诲姟闃熷垪涓?/p>


瀹忎换鍔¢槦鍒楋細setTimeout2

寰换鍔¢槦鍒楋細process2銆?then2



绗簩杞畯浠诲姟鎵ц瀹屾瘯锛屾墦鍗板嚭b鍜宒銆?/p>

鏌ユ壘寰换鍔¢槦鍒椾腑鏈塸rocess2鍜宼hen2銆傚叏閮ㄦ墽琛岋紝鎵撳嵃c鍜宔銆?/p>

绗簩杞簨浠跺惊鐜畬姣曪紝鎵撳嵃鍑篵銆乨銆乧鍜宔銆?/p>


绗笁杞簨浠跺惊鐜細


1.鎵цsetTimeout2锛岄亣鍒癱onsole.log('i')锛屾墦鍗癷銆?/p>

2.閬囧埌process.nextTick()锛岃繘鍏ュ井浠诲姟闃熷垪锛屾殏瀹氫箟涓簆rocess3銆?/p>

3.new Promise鐩存帴鎵ц锛屾墦鍗発銆?/p>

4.then杩涘叆寰换鍔¢槦鍒楋紝鏆傚畾涔変负then3銆?/p>


姝ゆ椂涓や釜浠诲姟闃熷垪涓?/p>


瀹忎换鍔¢槦鍒楋細绌?/p>

寰换鍔¢槦鍒楋細process3銆乼hen3


绗笁杞畯浠诲姟鎵ц瀹屾瘯锛屾墦鍗板嚭i鍜宬銆?/p>

鏌ユ壘寰换鍔¢槦鍒椾腑鏈塸rocess3鍜宼hen3銆傚叏閮ㄦ墽琛岋紝鎵撳嵃j鍜宭銆?/p>

绗笁杞簨浠跺惊鐜畬姣曪紝鎵撳嵃鍑篿銆乲銆乯鍜宭銆?/p>

鍒版涓烘锛屼笁杞簨浠跺惊鐜畬姣曪紝鏈€缁堣緭鍑虹粨鏋滀负锛?/p>


a銆乬銆乫銆乭銆乥銆乨銆乧銆乪銆乮銆乲銆乯銆乴


l鏈€鎸佷箙锛屼綘绛斿浜嗗悧锛?/p>


浠ヤ笂浠g爜浠呭湪娴忚鍣ㄧ幆澧冧腑鎵ц椤哄簭濡備笅锛?span>node鐜涓嬪彲鑳藉瓨鍦ㄤ笉鍚?/strong>銆?/p>


鐪嬪畬鏈枃甯屾湜浣犺兘澶熺悊瑙avaScript寮曟搸鐨凟vent Loop鎵ц鏈哄埗锛屼笉浠呭彲浠ヨ鎴戜滑鏇村姞娣卞埢鐨勮璇咼avaScript杩欓棬璇█锛岃€屼笖闈㈣瘯琚棶璧风殑鏃跺€欏彲浠ュ拰闈㈣瘯瀹樹緝渚冭€岃皥銆?/p>


濡傛灉瑙夊緱鐪嬪畬鏈枃鏈夋墍鏀惰幏锛岃鐐瑰嚮鍙充笅瑙掑湪鐪嬫敮鎸侊紝鎰熻阿鏀寔銆?/p>

以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数