娣卞叆娴呭嚭JavaScript杩愯鏈哄埗

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了娣卞叆娴呭嚭JavaScript杩愯鏈哄埗相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%9c%b0%e6%96%b9' title='鍦版柟'>鍦版柟   out   鍝簺   涓€浜涗簨   etl   绯荤粺   async   

涓€銆佸紩瀛?/h2>

鏈枃浠嬬粛JavaScript杩愯鏈哄埗锛岃繖涓€閮ㄥ垎姣旇緝鎶借薄锛屾垜浠厛浠庝竴閬撻潰璇曢鍏ユ墜锛?/p>

console.log(1);
setTimeout(function(){
console.log(3);
},0);
console.log(2);
璇烽棶鏁板瓧鎵撳嵃椤哄簭鏄粈涔堬紵

杩欎竴棰樼湅浼煎緢绠€鍗曪紝浣嗗鏋滀綘涓嶄簡瑙avaScript杩愯鏈哄埗锛屽緢瀹规槗灏辩瓟閿欎簡銆傞鐩殑绛旀鏄緷娆¤緭鍑? 2 3锛屽鏋滀綘鏈夌枒鎯戯紝涓嬫枃鏈夎缁嗚В閲娿€?/p>

浜屻€佺悊瑙S鐨勫崟绾跨▼鐨勬蹇?/h2>

javascript璇█鐨勪竴澶х壒鐐瑰氨鏄崟绾跨▼锛屼篃灏辨槸璇达紝鍚屼竴涓椂闂村彧鑳藉仛涓€浠朵簨銆傞偅涔堬紝涓轰粈涔圝avaScript涓嶈兘鏈夊涓嚎绋嬪憿锛熻繖鏍疯兘鎻愰珮鏁堢巼鍟娿€?br>JavaScript鐨勫崟绾跨▼锛屼笌瀹冪殑鐢ㄩ€旀湁鍏炽€備綔涓烘祻瑙堝櫒鑴氭湰璇█锛孞avaScript鐨勪富瑕佺敤閫旀槸涓庣敤鎴蜂簰鍔紝浠ュ強鎿嶄綔DOM銆傝繖鍐冲畾浜嗗畠鍙兘鏄崟绾跨▼锛屽惁鍒欎細甯︽潵寰堝鏉傜殑鍚屾闂銆傛瘮濡傦紝鍋囧畾JavaScript鍚屾椂鏈変袱涓嚎绋嬶紝涓€涓嚎绋嬪湪鏌愪釜DOM鑺傜偣涓婃坊鍔犲唴瀹癸紝鍙︿竴涓嚎绋嬪垹闄や簡杩欎釜鑺傜偣锛岃繖鏃舵祻瑙堝櫒搴旇浠ュ摢涓嚎绋嬩负鍑嗭紵
鎵€浠ワ紝涓轰簡閬垮厤澶嶆潅鎬э紝浠庝竴璇炵敓锛孞avaScript灏辨槸鍗曠嚎绋嬶紝杩欏凡缁忔垚浜嗚繖闂ㄨ瑷€鐨勬牳蹇冪壒寰侊紝灏嗘潵涔熶笉浼氭敼鍙樸€?/p>

涓夈€佺悊瑙d换鍔¢槦鍒?娑堟伅闃熷垪)

鍗曠嚎绋嬪氨鎰忓懗鐫€锛屾墍鏈変换鍔¢渶瑕佹帓闃燂紝鍓嶄竴涓换鍔$粨鏉燂紝鎵嶄細鎵ц鍚庝竴涓换鍔°€傚鏋滃墠涓€涓换鍔¤€楁椂寰堥暱锛屽悗涓€涓换鍔″氨涓嶅緱涓嶄竴鐩寸瓑鐫€銆侸avaScript璇█鐨勮璁¤€呮剰璇嗗埌杩欎釜闂锛屽皢鎵€鏈変换鍔″垎鎴愪袱绉嶏紝涓€绉嶆槸鍚屾浠诲姟锛坰ynchronous锛夛紝鍙︿竴绉嶆槸寮傛浠诲姟锛坅synchronous锛?/strong>銆傚悓姝ヤ换鍔℃寚鐨勬槸锛屽湪涓荤嚎绋嬩笂鎺掗槦鎵ц鐨勪换鍔★紝鍙湁鍓嶄竴涓换鍔℃墽琛屽畬姣曪紝鎵嶈兘鎵ц鍚庝竴涓换鍔★紱寮傛浠诲姟鎸囩殑鏄紝涓嶈繘鍏ヤ富绾跨▼銆佽€岃繘鍏?浠诲姟闃熷垪"锛坱ask queue锛夌殑浠诲姟锛屽彧鏈?浠诲姟闃熷垪"閫氱煡涓荤嚎绋嬶紝鏌愪釜寮傛浠诲姟鍙互鎵ц浜嗭紝璇ヤ换鍔℃墠浼氳繘鍏ヤ富绾跨▼鎵ц銆?strong>寮傛浠诲姟鍖呮嫭瀹忎换鍔″拰寰换鍔?鍚庨潰浼氶噸鐐逛粙缁?锛屾帴涓嬫潵鎴戜滑閫氳繃涓や釜渚嬪瓙璇存槑鍚屾浠诲姟鍜屽紓姝ヤ换鍔$殑鍖哄埆锛?/p>

console.log("A");
while(true){ }
console.log("B");
璇烽棶鏈€鍚庣殑杈撳嚭缁撴灉鏄粈涔堬紵

濡傛灉浣犵殑鍥炵瓟鏄疉,鎭枩浣犵瓟瀵逛簡锛屽洜涓鸿繖鏄悓姝ヤ换鍔★紝绋嬪簭鐢变笂鍒颁笅鎵ц锛岄亣鍒皐hile()姝诲惊鐜紝涓嬮潰璇彞灏辨病鍔炴硶鎵ц銆?/p>

console.log("A");
setTimeout(function(){
console.log("B");
},0);
while(true){}
璇烽棶鏈€鍚庣殑杈撳嚭缁撴灉鏄粈涔堬紵

濡傛灉浣犵殑绛旀鏄疉锛屾伃鍠滀綘鐜板湪瀵筳s杩愯鏈哄埗宸茬粡鏈変釜绮楁祬鐨勮璇嗕簡锛侀鐩腑鐨剆etTimeout()灏辨槸涓紓姝ヤ换鍔°€?strong>鍦ㄦ墍鏈夊悓姝ヤ换鍔℃墽琛屽畬涔嬪墠锛屼换浣曠殑寮傛浠诲姟鏄笉浼氭墽琛岀殑锛屽叧浜庤繖鐐逛笅鏂囪繕浼氳缁嗚鏄庛€?/p>

鍥涖€佺悊瑙vent Loop

寮傛鎵ц鐨勮繍琛屾満鍒跺涓嬶細

  1. 鎵€鏈夊悓姝ヤ换鍔¢兘鍦ㄤ富绾跨▼涓婃墽琛岋紝褰㈡垚涓€涓墽琛屾爤锛坋xecution context stack锛夈€?/li>
  2. 涓荤嚎绋嬩箣澶栵紝杩樺瓨鍦ㄤ竴涓?浠诲姟闃熷垪"锛坱ask queue锛夈€傚彧瑕佸紓姝ヤ换鍔℃湁浜嗚繍琛岀粨鏋滐紝灏卞湪"浠诲姟闃熷垪"涔嬩腑鏀剧疆涓€涓簨浠躲€?/li>
  3. 涓€鏃?鎵ц鏍?涓殑鎵€鏈夊悓姝ヤ换鍔℃墽琛屽畬姣曪紝绯荤粺灏变細璇诲彇"浠诲姟闃熷垪"锛岀湅鐪嬮噷闈㈡湁鍝簺浜嬩欢銆傞偅浜涘搴旂殑寮傛浠诲姟锛屼簬鏄粨鏉熺瓑寰呯姸鎬侊紝杩涘叆鎵ц鏍堬紝寮€濮嬫墽琛屻€?/li>
  4. 涓荤嚎绋嬩笉鏂噸澶嶄笂闈㈢殑绗笁姝ャ€?/li>

涓荤嚎绋嬩粠"浠诲姟闃熷垪"涓鍙栦簨浠讹紝杩欎釜杩囩▼鏄惊鐜笉鏂殑锛屾墍浠ユ暣涓殑杩欑杩愯鏈哄埗鍙堢О涓篍vent Loop锛堜簨浠跺惊鐜級銆傚彧瑕佷富绾跨▼绌轰簡锛屽氨浼氬幓璇诲彇"浠诲姟闃熷垪"锛岃繖灏辨槸JavaScript鐨勮繍琛屾満鍒?/strong>銆傝繖涓繃绋嬩細寰幆鍙嶅銆備互涓嬭繖寮犲浘鍙互寰堝ソ璇存槑杩欑偣銆?br>鎶€鏈浘鐗?  src=

浜斻€佸摢浜涜鍙ヤ細鏀惧叆寮傛浠诲姟闃熷垪鍙婃斁鍏ユ椂鏈?/h2>

涓€鑸潵璇达紝鏈変互涓嬪洓绉嶄細鏀惧叆寮傛浠诲姟闃熷垪锛?/p>

  1. setTimeout鍜宻etlnterval
  2. DOM浜嬩欢
  3. ES6涓殑Promise
  4. Ajax寮傛璇锋眰

javascript 浠g爜杩愯鍒嗕袱涓樁娈?/strong>锛?/p>

1銆侀瑙f瀽---鎶婃墍鏈夌殑鍑芥暟瀹氫箟鎻愬墠锛屾墍鏈夌殑鍙橀噺澹版槑鎻愬墠锛屽彉閲忕殑璧嬪€间笉鎻愬墠

2銆佹墽琛?--浠庝笂鍒颁笅鎵ц锛堟寜鐓s杩愯鏈哄埗锛?/strong>

鑷充簬鏀惧叆寮傛浠诲姟闃熷垪鐨勬椂鏈猴紝鎴戜滑閫氳繃 setTimeout鐨勪緥瀛愬拰Ajax渚嬪瓙鏉ヨ缁嗚鏄庯細

渚嬮1
for (var i = 0; i < 5; i++) {
setTimeout(function() {  
 console.log(i);  
  }, 1000);
}
璇烽棶鏈€鍚庣殑杈撳嚭缁撴灉鏄粈涔堬紵

for寰幆涓€娆$鍒颁竴涓?setTimeout()锛?strong>骞朵笉鏄┈涓婃妸setTimeout()鎷垮埌寮傛闃熷垪涓紝鑰岃绛夊埌涓€绉掑悗锛屾墠灏嗗叾鏀惧埌浠诲姟闃熷垪閲岄潰锛屼竴鏃?鎵ц鏍?涓殑鎵€鏈夊悓姝ヤ换鍔℃墽琛屽畬姣曪紙鍗砯or寰幆缁撴潫锛屾鏃秈宸茬粡涓?锛夛紝绯荤粺灏变細璇诲彇宸茬粡瀛樻斁"浠诲姟闃熷垪"鐨剆etTimeout()锛堟湁浜斾釜锛夛紝浜庢槸绛旀鏄緭鍑?涓?銆?/p>

涓婇潰涔熸彁鍒帮紝鍦ㄥ埌杈炬寚瀹氭椂闂存椂锛屽畾鏃跺櫒灏变細灏嗙浉搴斿洖璋冨嚱鏁版彃鍏モ€滀换鍔¢槦鍒椻€濆熬閮ㄣ€傝繖灏辨槸鈥滃畾鏃跺櫒锛坱imer锛夆€濆姛鑳?/strong>銆?/p>

鍏充簬瀹氭椂鍣ㄧ殑閲嶈琛ュ厖锛?/p>

瀹氭椂鍣ㄥ寘鎷瑂etTimeout涓?setInterval 涓や釜鏂规硶銆傚畠浠殑绗簩涓弬鏁版槸鎸囧畾鍏跺洖璋冨嚱鏁版帹杩?姣忛殧澶氬皯姣鏁板悗鎵ц銆?/p>

瀵逛簬绗簩涓弬鏁版湁浠ヤ笅闇€瑕佹敞鎰忕殑鍦版柟锛?/p>

褰撶浜屼釜鍙傛暟缂虹渷鏃讹紝榛樿涓?0锛?/p>

褰撴寚瀹氱殑鍊煎皬浜?4 姣锛屽垯澧炲姞鍒?4ms(4ms 鏄?HTML5 鏍囧噯鎸囧畾鐨勶紝瀵逛簬 2010 骞村強涔嬪墠鐨勬祻瑙堝櫒鍒欐槸 10ms);涔熷氨鏄鑷冲皯闇€瑕?姣锛岃setTimeout()鎷垮埌浠诲姟闃熷垪涓€?/p>

渚嬮2
$.ajax({
url锛氣€渪xxxx",
success:function (result){
console.log("a")
   }
})
setTimeout(function (){
console.log("b")
},100)
setTimeout(function (){
console.log("c")
})
console.log("d");

鎶€鏈浘鐗?  src=

ajax鍔犺浇瀹屾垚鏃舵墠浼氭斁鍏ュ紓姝ラ槦鍒楋紝鑷充簬杩欐鏃堕棿涓嶇‘瀹氾紝鎵€鏈夋湁涓ょ鎯呭喌锛氣憼澶т簬100ms,鏈€鍚庣殑缁撴灉鏄?d c b a ;鈶″皬浜?00ms,鏈€鍚庣殑缁撴灉渚挎槸d c a b銆?/p>

鍏€佸井浠诲姟(Microtask)涓庡畯浠诲姟(Macrotask)

鎴戜滑涓婇潰鎻愬埌寮傛浠诲姟鍒嗕负瀹忎换鍔″拰寰换鍔★紝瀹忎换鍔¢槦鍒楀彲浠ユ湁澶氫釜锛屽井浠诲姟闃熷垪鍙湁涓€涓?/strong>銆?/p>

  • 瀹忎换鍔″寘鎷細script(鍏ㄥ眬浠诲姟), setTimeout, setInterval, setImmediate, I/O, UI rendering銆?/li>
  • 寰换鍔″寘鎷? new Promise().then(鍥炶皟), process.nextTick, Object.observe(宸插簾寮?, MutationObserver(html5鏂扮壒鎬?

褰撴墽琛屾爤涓殑鎵€鏈夊悓姝ヤ换鍔℃墽琛屽畬姣曟椂锛屾槸鍏堟墽琛屽畯浠诲姟杩樻槸寰换鍔″憿锛?/strong>

  • 鐢变簬鎵ц浠g爜鍏ュ彛閮芥槸鍏ㄥ眬浠诲姟 script锛岃€屽叏灞€浠诲姟灞炰簬瀹忎换鍔★紝鎵€浠ュ綋鏍堜负绌猴紝鍚屾浠诲姟浠诲姟鎵ц瀹屾瘯鏃讹紝浼氬厛鎵ц寰换鍔¢槦鍒楅噷鐨勪换鍔°€?/li>
  • 寰换鍔¢槦鍒楅噷鐨勪换鍔″叏閮ㄦ墽琛屽畬姣曞悗锛屼細璇诲彇瀹忎换鍔¢槦鍒椾腑鎷嶆渶鍓嶇殑浠诲姟銆?/li>
  • 鎵ц瀹忎换鍔$殑杩囩▼涓紝閬囧埌寰换鍔★紝渚濇鍔犲叆寰换鍔¢槦鍒椼€?/li>
  • 鏍堢┖鍚庯紝鍐嶆璇诲彇寰换鍔¢槦鍒楅噷鐨勪换鍔★紝渚濇绫绘帹銆?/li>

鎶€鏈浘鐗?  src=

涓€鍙ヨ瘽姒傛嫭涓婇潰鐨勬祦绋嬪浘锛?strong>褰撴煇涓畯浠诲姟闃熷垪鐨勪腑鐨勪换鍔″叏閮ㄦ墽琛屽畬浠ュ悗,浼氭煡鐪嬫槸鍚︽湁寰换鍔¢槦鍒椼€傚鏋滄湁锛屽厛鎵ц寰换鍔¢槦鍒椾腑鐨勬墍鏈変换鍔★紝濡傛灉娌℃湁锛屽氨鏌ョ湅鏄惁鏈夊叾浠栧畯浠诲姟闃熷垪銆?/p>

鎺ヤ笅鏉ユ垜浠湅涓ら亾渚嬪瓙鏉ヤ粙缁嶄笂闈㈡祦绋嬶細

Promise.resolve().then(()=>{
  console.log(鈥楶romise1鈥?  
  setTimeout(()=>{
    console.log(鈥榮etTimeout2鈥?
  },0)
})
setTimeout(()=>{
  console.log(鈥榮etTimeout1鈥?
  Promise.resolve().then(()=>{
    console.log(鈥楶romise2鈥?    
  })
},0)

鏈€鍚庤緭鍑虹粨鏋滄槸Promise1锛宻etTimeout1锛孭romise2锛宻etTimeout2

  • 涓€寮€濮嬫墽琛屾爤鐨勫悓姝ヤ换鍔℃墽琛屽畬姣曪紝浼氬幓鏌ョ湅鏄惁鏈夊井浠诲姟闃熷垪锛屼笂棰樹腑瀛樺湪(鏈変笖鍙湁涓€涓?锛岀劧鍚庢墽琛屽井浠诲姟闃熷垪涓殑鎵€鏈変换鍔¤緭鍑篜romise1锛屽悓鏃朵細鐢熸垚涓€涓畯浠诲姟 setTimeout2
  • 鐒跺悗鍘绘煡鐪嬪畯浠诲姟闃熷垪锛屽畯浠诲姟 setTimeout1 鍦?setTimeout2 涔嬪墠锛屽厛鎵ц瀹忎换鍔?setTimeout1锛岃緭鍑?setTimeout1
  • 鍦ㄦ墽琛屽畯浠诲姟setTimeout1鏃朵細鐢熸垚寰换鍔romise2 锛屾斁鍏ュ井浠诲姟闃熷垪涓紝鎺ョ潃鍏堝幓娓呯┖寰换鍔¢槦鍒椾腑鐨勬墍鏈変换鍔★紝杈撳嚭 Promise2
  • 娓呯┖瀹屽井浠诲姟闃熷垪涓殑鎵€鏈変换鍔″悗锛屽氨鍙堜細鍘诲畯浠诲姟闃熷垪鍙栦竴涓紝杩欏洖鎵ц鐨勬槸 setTimeout2
console.log(鈥?---------------- start -----------------鈥?;
setTimeout(() => {
  console.log(鈥榮etTimeout鈥?;
}, 0)
new Promise((resolve, reject) =>{
  for (var i = 0; i < 5; i++) {
    console.log(i);
  }
  resolve();  // 淇敼promise瀹炰緥瀵硅薄鐨勭姸鎬佷负鎴愬姛鐨勭姸鎬?}).then(() => {
  console.log(鈥榩romise瀹炰緥鎴愬姛鍥炶皟鎵ц鈥?;
})
console.log(鈥?---------------- end -----------------鈥?;

鎶€鏈浘鐗?  src=

涓冦€侀澶栬瘽

濡傛灉瑕佽緭鍑?~4锛屼笂闈緥棰樺簲璇ュ浣曚慨鏀癸紵

  1. 灏唙ar鍙樹负let
for (let i = 0; i < 5; i++) {
setTimeout(function() {  
  console.log(i);
  }, 1000);
}

2.鍔犱釜绔嬪嵆鎵ц鍑芥暟

for (var i = 0; i < 5; i++) {
(function(i){
setTimeout(function() {  
  console.log(i);
  }, 1000);
})(i)
}

3.涔熷彲浠ラ€氳繃杩欐牱鍔犻棴鍖?/p>

for(var i = 1;i < 5;i++){  
  var a = function(){  
      var j = i;    
    setTimeout(function(){  
          console.log(j);  
      },1000)  
  }    
a();
}

鏂囩珷浜?018.11.15閲嶆柊淇敼锛屽鏋滆寰楁枃绔犲浣犳湁浜涜甯姪锛屾杩庡湪鎴戠殑GitHub鍗氬鐐硅禐鍜屽叧娉紝鎰熸縺涓嶅敖锛?/strong>

鍏€佸弬鑰冭祫鏂?/h2>

JavaScript 杩愯鏈哄埗璇﹁В锛氬啀璋圗vent Loop

鍏充簬JavaScript鍗曠嚎绋嬬殑涓€浜涗簨

JS浜嬩欢寰幆鏈哄埗锛坋vent loop锛変箣瀹忎换鍔?寰换鍔?/a>

鍓嶇鍩虹杩涢樁锛堝崄浜岋級锛氭繁鍏ユ牳蹇冿紝璇﹁В浜嬩欢寰幆鏈哄埗

浜嬩欢寰幆鏈哄埗鐨勯偅浜涗簨


以上是关于娣卞叆娴呭嚭JavaScript杩愯鏈哄埗的主要内容,如果未能解决你的问题,请参考以下文章

娣卞叆娴呭嚭璁$畻鏈虹粍鎴愬師鐞嗭細鏁版嵁瀹屾暣鎬э紙涓嬶級-濡備綍杩樺師鐘姜鐜板満锛堢50璁诧級

[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑

娴呭嚭GEOHASH绠楁硶

Cookie/Session鏈哄埗璇﹁В