鍏充簬setTimeout()浣犳墍涓嶇煡閬撶殑鍦版柟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍏充簬setTimeout()浣犳墍涓嶇煡閬撶殑鍦版柟相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

鍓嶈█锛氱湅浜嗚繖绡囨枃绔狅紝1.娉ㄦ剰setTimeout寮曠敤鐨勬槸鍏ㄩ儴鍙橀噺杩樻槸灞€閮ㄥ彉閲忎簡锛屽綋鐩存帴璋冪敤澶栭儴鍑芥暟鏂规硶鏃讹紝瀹為檯涓婂嚱鏁板唴閮ㄧ殑鍙橀噺宸茬粡鍙樻垚鍏?灞€銆?.鎻愰啋鎴戦槻姝㈠嚭閿欑殑锛岀敤鍖垮悕鍑芥暟涓嶅鏄撳嚭閿欍€?.setTimeout鏈€蹇墽琛屾椂闂存槸4姣銆?.浣跨敤setTimeout鐨勫嚱鏁颁細姣斿綋鍓嶉槦鍒楃殑鍑?鏁版參鎵ц锛屽湪浠栦滑鐨勫悗闈€?.setTimeout涓殑瀹氭椂鍣ㄥ苟涓嶆槸鍑嗙‘鐨勬椂闂达紝瀹為檯涓畠闇€瑕佸湪鎵ц瀹屽墠闈㈢殑鍑芥暟鍚庢墠瀹氭椂鎵ц銆?/p>

浣犺繕鍙互鏌ラ槄鍏充簬setTimeout鐨勭浉鍏充粙缁?/a>

javascript鎻愪緵瀹氭椂鎵ц浠g爜鐨勫姛鑳斤紝鍙仛瀹氭椂鍣紙timer锛夛紝涓昏鐢眘etTimeout()鍜宻etInterval()杩欎袱涓嚱鏁版潵瀹屾垚銆傚畠浠悜浠诲姟闃熷垪娣诲姞瀹氭椂浠诲姟銆傚垵濮嬫帴瑙﹀畠鐨勪汉閮借寰楀ソ绠€鍗曪紝瀹炴椂涓婄湡鐨勫姝や箞锛熻繖閲岃杞戒笅锛屼竴璺鍏朵娇鐢ㄥЭ鍔垮彉杩佺殑鍘嗙▼銆?/span>

1, setTimeout()鍩虹

setTimeout鍑芥暟鐢ㄦ潵鎸囧畾鏌愪釜鍑芥暟鎴栨煇娈典唬鐮侊紝鍦ㄥ灏戞绉掍箣鍚庢墽琛屻€傚畠杩斿洖涓€涓暣鏁帮紝琛ㄧず瀹氭椂鍣ㄧ殑缂栧彿锛屼互鍚庡彲浠ョ敤鏉ュ彇娑堣繖涓畾鏃跺櫒銆?/p>

var timerId = setTimeout(func|code, delay)

涓婇潰浠g爜涓紝setTimeout鍑芥暟鎺ュ彈涓や釜鍙傛暟锛岀涓€涓弬鏁癴unc|code鏄皢瑕佹帹杩熸墽琛岀殑鍑芥暟鍚嶆垨鑰呬竴娈典唬鐮侊紝绗簩涓弬鏁癲elay鏄帹杩熸墽琛岀殑姣鏁般€?/p>

console.log(1);
setTimeout(鈥榗onsole.log(2)鈥?1000);
console.log(3);

涓婇潰浠g爜鐨勮緭鍑虹粨鏋滃氨鏄?锛?锛?锛屽洜涓簊etTimeout鎸囧畾绗簩琛岃鍙ユ帹杩?000姣鍐嶆墽琛?濡傛灉杩欏湪Sublime涓嬭繍鐢ㄦ彃浠朵互nodejs鐜鏉ユ墽琛岋紝璁歌В閲婂櫒涓嶅悓锛屼細鎶ラ敊)銆?/p>

闇€瑕佹敞鎰忕殑鏄紝鎺ㄨ繜鎵ц鐨勪唬鐮佸繀椤讳互瀛楃涓茬殑褰㈠紡锛屾斁鍏etTimeout锛屽洜涓哄紩鎿庡唴閮ㄤ娇鐢╡val鍑芥暟锛屽皢瀛楃涓茶浆涓轰唬鐮併€傚鏋?strong>鎺ㄨ繜鎵ц鐨勬槸鍑芥暟锛屽垯鍙互鐩存帴灏嗗嚱鏁板悕锛屾斁鍏etTimeout銆備竴鏂归潰eval鍑芥暟鏈夊畨鍏ㄩ【铏戯紝鍙︿竴鏂归潰涓轰簡渚夸簬JavaScript寮曟搸浼樺寲浠g爜锛宻etTimeout鏂规硶涓€鑸€绘槸閲囩敤鍑芥暟鍚嶇殑褰㈠紡锛屽氨鍍忎笅闈㈣繖鏍枫€?/span>

function func(){
  console.log(2);
}
setTimeout(func,1000);
// 鎴栬€?
setTimeout(function (){console.log(2)},1000);

setTimeout浼犲弬鏁?/strong>

闄や簡鍓嶄袱涓弬鏁帮紝setTimeout杩樺厑璁告坊鍔犳洿澶氱殑鍙傛暟銆傚畠浠皢琚紶鍏ユ帹杩熸墽琛岀殑鍑芥暟锛堝洖璋冨嚱鏁帮級銆?/p>

setTimeout(function(a,b){
  console.log(a+b);
},1000,1,1);

涓婇潰浠g爜涓紝setTimeout鍏辨湁4涓弬鏁般€傛渶鍚庨偅涓や釜鍙傛暟锛屽皢鍦?000姣涔嬪悗鍥炶皟鍑芥暟鎵ц鏃讹紝浣滀负鍥炶皟鍑芥暟鐨勫弬鏁般€?/p>

IE 9.0鍙婁互涓嬬増鏈紝鍙厑璁竤etTimeout鏈変袱涓弬鏁帮紝涓嶆敮鎸佹洿澶氱殑鍙傛暟;鍙互鍦ㄥ尶鍚嶅嚱鏁颁腑锛岃鍥炶皟鍑芥暟甯﹀弬鏁拌繍琛岋紝鍐嶆妸鍖垮悕鍑芥暟杈撳叆setTimeout锛涗緥濡?

setTimeout(function() {
  myFunc("one", "two", "three");
}, 1000);

褰撶劧涔熷彲浠ヤ娇鐢?strong>bind鎴?strong>apply鏂规硶鏉ヨВ鍐炽€?/p>

渚嬪浣跨敤bind鏂规硶锛屾妸澶氫綑鐨勫弬鏁扮粦瀹氬湪鍥炶皟鍑芥暟涓婇潰锛岀敓鎴愪竴涓柊鐨勫嚱鏁拌緭鍏etTimeout銆?/p>

setTimeout( function(arg1){}.bind(undefined, 10), 1000 );

涓婇潰浠g爜涓紝bind鏂规硶绗竴涓弬鏁版槸undefined锛岃〃绀哄皢鍘熷嚱鏁扮殑this缁戝畾鍏ㄥ眬浣滅敤鍩燂紝绗簩涓弬鏁版槸瑕佷紶鍏ュ師鍑芥暟鐨勫弬鏁般€傚畠杩愯鍚庝細杩斿洖涓€涓柊鍑芥暟锛岃鍑芥暟涓嶅甫鍙傛暟銆?/p>

setTimeout娉ㄦ剰鐐?/strong>

setTimeout()涓洖璋冨嚱鏁颁腑鐨則his
濡傛灉琚玸etTimeout鎺ㄨ繜鎵ц鐨勫洖璋冨嚱鏁版槸鏌愪釜瀵硅薄鐨勬柟娉曪紝閭d箞璇ユ柟娉曚腑鐨則his鍏抽敭瀛楀皢鎸囧悜鍏ㄥ眬鐜锛岃€屼笉鏄畾涔夋椂鎵€鍦ㄧ殑閭d釜瀵硅薄銆?/p>

var x = 1;

var o = {
  x: 2,
  y: function(){
    console.log(this.x);
  }
};
setTimeout(o.y,1000);// 1

涓婇潰浠g爜杈撳嚭鐨勬槸1锛岃€屼笉鏄?锛岃繖琛ㄧずo.y鐨則his鎵€鎸囧悜鐨勫凡缁忎笉鏄痮锛岃€屾槸鍏ㄥ眬鐜浜嗐€?/p>

鍐嶇湅涓€涓笉瀹规槗鍙戠幇閿欒鐨勪緥瀛愩€?/p>

function User(login) {
  this.login = login;
  this.sayHi = function() {
    console.log(this.login);
  }
}
var user = new User(鈥楯ohn鈥?;
setTimeout(user.sayHi, 1000);

涓婇潰浠g爜鍙細鏄剧ずundefined锛屽洜涓虹瓑鍒皍ser.sayHi鎵ц鏃讹紝瀹冩槸鍦?strong>鍏ㄥ眬瀵硅薄涓墽琛?/strong>锛屾墍浠his.login鍙栦笉鍒板€笺€?/p>

涓轰簡闃叉鍑虹幇杩欎釜闂锛屼竴绉嶈В鍐虫柟娉曟槸灏唘ser.sayHi鏀惧湪鍖垮悕鍑芥暟涓墽琛屻€?/p>

setTimeout(function() {
  user.sayHi();
}, 1000);

涓婇潰浠g爜涓紝sayHi鏄湪user浣滅敤鍩熷唴鎵ц锛岃€屼笉鏄湪鍏ㄥ眬浣滅敤鍩熷唴鎵ц锛屾墍浠ヨ兘澶熸樉绀烘纭殑鍊笺€?/p>

鍙︿竴绉嶈В鍐虫柟娉曟槸锛屼娇鐢╞ind鏂规硶锛屽皢缁戝畾sayHi缁戝畾鍦╱ser涓婇潰銆?/p>

setTimeout(user.sayHi.bind(user), 1000);

html 5鏍囧噯瑙勫畾锛宻etTimeout鐨勬渶鐭椂闂撮棿闅旀槸4姣銆備负浜嗚妭鐢碉紝瀵逛簬閭d簺涓嶅浜庡綋鍓嶇獥鍙g殑椤甸潰锛屾祻瑙堝櫒浼氬皢鏃堕棿闂撮殧鎵╁ぇ鍒?000姣銆傚彟澶栵紝濡傛灉 绗旇鏈數鑴戝浜庣數姹犱緵鐢电姸鎬侊紝Chrome鍜孖E 9浠ヤ笂鐨勭増鏈紝浼氬皢鏃堕棿闂撮殧鍒囨崲鍒扮郴缁熷畾鏃跺櫒锛屽ぇ绾︽槸15.6姣銆?/span>

setTimeout鎵ц鍥炶皟闂撮殧鏃堕棿闀垮害
濡傛灉浣犲湪涓€娈典唬鐮佷腑鍙戠幇涓嬮潰鍐呭:

var startTime = new Date();
setTimeout(function () {
    console.log(new Date() - startTime);
}, 100)

璇烽棶鏈€鍚庢墦鍗扮殑鏄灏?鍏舵纭瓟妗堟槸锛屽彇鍐充簬鍚庨潰鍚屾鎵ц鐨?span class="wp_keywordlink">JS闇€瑕佸崰鐢ㄥ灏戞椂闂淬€?br /> 鍗充负锛歁AX(鍚屾鎵ц鐨勬椂闂? 100)锛涚紭浣曞姝わ紝灏卞緱鐪嬩笅setTimeout杩愯鏈哄埗浜嗐€?/span>

2, setTimeout杩愯鏈哄埗

setTimeout鍜宻etInterval鐨勮繍琛屾満鍒舵槸锛屽皢鎸囧畾鐨勪唬鐮佺Щ鍑烘湰娆℃墽琛岋紝绛夊埌涓嬩竴杞瓻vent Loop鏃讹紝鍐嶆鏌ユ槸鍚﹀埌浜嗘寚瀹氭椂闂淬€傚鏋滃埌浜嗭紝灏辨墽琛屽搴旂殑浠g爜锛涘鏋滀笉鍒帮紝灏辩瓑鍒板啀涓嬩竴杞瓻vent Loop鏃堕噸鏂板垽鏂€傝繖鎰忓懗鐫€锛宻etTimeout鎸囧畾鐨勪唬鐮侊紝蹇呴』绛夊埌鏈鎵ц鐨勬墍鏈変唬鐮侀兘鎵ц瀹岋紝鎵嶄細鎵ц銆?/p>

姣忎竴杞瓻vent Loop鏃讹紝閮戒細灏?ldquo;浠诲姟闃熷垪”涓渶瑕佹墽琛岀殑浠诲姟锛屼竴娆℃墽琛屽畬銆俿etTimeout鍜宻etInterval閮芥槸鎶婁换鍔℃坊鍔犲埌“浠诲姟闃熷垪”鐨勫熬閮ㄣ€傚洜 姝わ紝瀹冧滑瀹為檯涓婅绛夊埌褰撳墠鑴氭湰鐨勬墍鏈夊悓姝ヤ换鍔℃墽琛屽畬锛岀劧鍚庡啀绛夊埌鏈Event Loop鐨?ldquo;浠诲姟闃熷垪”鐨勬墍鏈変换鍔℃墽琛屽畬锛屾墠浼氬紑濮嬫墽琛屻€傜敱浜庡墠闈㈢殑浠诲姟鍒板簳闇€瑕佸灏戞椂闂存墽琛屽畬锛屾槸涓嶇‘瀹氱殑锛屾墍浠ユ病鏈夊姙娉曚繚璇侊紝setTimeout鍜? setInterval鎸囧畾鐨勪换鍔★紝涓€瀹氫細鎸夌収棰勫畾鏃堕棿鎵ц銆?/p>

setTimeout(someTask,100);
veryLongTask();

涓婇潰浠g爜鐨剆etTimeout锛屾寚瀹?00姣浠ュ悗杩愯涓€涓换鍔°€備絾鏄紝濡傛灉鍚庨潰绔嬪嵆杩愯鐨勪换鍔★紙褰撳墠鑴氭湰鐨勫悓姝ヤ换鍔★級锛夐潪甯歌€楁椂锛岃繃浜?00姣杩樻棤娉曠粨鏉燂紝閭d箞琚帹杩熻繍琛岀殑someTask灏卞彧鏈夌瓑鐫€锛岀瓑鍒板墠闈㈢殑veryLongTask杩愯缁撴潫锛屾墠杞埌瀹冩墽琛屻€?/p>

3, setTimeout(func,0)

鍦ㄤ娇鐢╞ackbone妗嗘灦鍐欎唬鐮佺殑鏃跺€欙紝鍥犱负浜涢渶姹傚洜绱狅紝鏂版墜鎬讳細鍦╮ender鏃舵搷绾典笅dom锛屽嵈鍙戠幇鏀瑰彉dom鍏冪礌鐘舵€侊紝浠g爜娌℃湁闂锛岀晫闈?鍗存病鏈夊彉鏇淬€傝€屼娇鐢╯etTimeout(func,time)鍗磋兘瑙e喅杩欎釜闂锛屽嵆渚縯ime=0;鎺㈢┒涓€鐣紝鐪熺浉鍙湁涓€涓細

setTimeout(func,0)鍚箟

杩愯涓嬮潰浠o紝func1鍜宖unc2璋佷細鍏堟墽琛岋紵寰堟槑鏄緁unc2鍏堟墽琛岋紱

setTimeout(function () {
    func1();
}, 0)
func2();

setTimeout鐨勪綔鐢ㄦ槸灏嗕唬鐮佹帹杩熷埌鎸囧畾鏃堕棿鎵ц锛屽鏋滄寚瀹氭椂闂翠负0锛屽嵆setTimeout(f,0)锛岄偅涔堜細绔嬪埢鎵ц鍚楋紵

绛旀鏄笉浼氥€傚洜涓?strong>setTimeout杩愯鏈哄埗璇磋繃锛屽繀椤昏绛夊埌褰撳墠鑴氭湰鐨勫悓姝ヤ换鍔″拰“浠诲姟闃熷垪”涓凡鏈夌殑浜?浠讹紝鍏ㄩ儴澶勭悊瀹屼互鍚庯紝鎵嶄細鎵цsetTimeout鎸囧畾鐨勪换鍔°€備篃灏辨槸璇达紝setTimeout鐨勭湡姝d綔鐢ㄦ槸锛屽湪“浠诲姟闃熷垪”鐨勭幇鏈変簨浠剁殑鍚庨潰鍐嶆坊鍔犱竴涓?浜嬩欢锛岃瀹氬湪鎸囧畾鏃堕棿鎵ц鏌愭浠g爜銆俿etTimeout娣诲姞鐨勪簨浠讹紝浼氬湪涓嬩竴娆vent Loop鎵ц銆?/p>

setTimeout(f,0)灏嗙浜屼釜鍙傛暟璁句负0锛屼綔鐢ㄦ槸璁ゝ鍦ㄧ幇鏈夌殑浠诲姟锛堣剼鏈殑鍚屾浠诲姟鍜?ldquo;浠诲姟闃熷垪”涓凡鏈夌殑浜嬩欢锛変竴缁撴潫灏辩珛鍒绘墽琛屻€備篃灏辨槸璇达紝setTimeout(f,0)鐨勪綔鐢ㄦ槸锛屽敖鍙兘鏃╁湴鎵ц鎸囧畾鐨勪换鍔°€?/p>

setTimeout(function (){
  console.log("浣犲ソ锛?);
}, 0);

涓婇潰浠g爜鐨勫惈涔夋槸锛屽敖鍙兘鏃╁湴鏄剧ず“浣犲ソ锛?rdquo;銆?/p>

setTimeout(f,0)鎸囧畾鐨勪换鍔★紝鏈€鏃╀篃瑕佸埌涓嬩竴娆vent Loop鎵嶄細鎵ц銆傝鐪嬩笅闈㈢殑渚嬪瓙銆?/p>

setTimeout(function() {
  console.log("Timeout");
}, 0);

function a(x) {
  console.log("a() 寮€濮嬭繍琛?);
  b(x);
  console.log("a() 缁撴潫杩愯");
}

function b(y) {
  console.log("b() 寮€濮嬭繍琛?);
  console.log("浼犲叆鐨勫€间负" + y);
  console.log("b() 缁撴潫杩愯");
}

console.log("褰撳墠浠诲姟寮€濮?);
a(42);
console.log("褰撳墠浠诲姟缁撴潫");

// 褰撳墠浠诲姟寮€濮?
// a() 寮€濮嬭繍琛?
// b() 寮€濮嬭繍琛?
// 浼犲叆鐨勫€间负42
// b() 缁撴潫杩愯
// a() 缁撴潫杩愯
// 褰撳墠浠诲姟缁撴潫
// Timeout

涓婇潰浠g爜璇存槑锛宻etTimeout(f,0)蹇呴』瑕佺瓑鍒板綋鍓嶈剼鏈殑鎵€鏈夊悓姝ヤ换鍔$粨鏉熷悗鎵嶄細鎵ц銆?/p>

0姣瀹為檯涓婅揪涓嶅埌鐨勩€傛牴鎹?a href="https://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers" target="_blank">HTML5鏍囧噯锛宻etTimeOut鎺ㄨ繜鎵ц鐨勬椂闂达紝鏈€灏戞槸4姣銆傚鏋滃皬浜庤繖涓€硷紝浼氳鑷姩澧炲姞鍒?銆傝繖鏄负浜嗛槻姝㈠涓?strong>setTimeout(f,0)璇彞杩炵画鎵ц锛岄€犳垚鎬ц兘闂銆?/p>

鍙︿竴鏂归潰锛屾祻瑙堝櫒鍐呴儴浣跨敤32浣嶅甫绗﹀彿鐨勬暣鏁帮紝鏉ュ偍瀛樻帹杩熸墽琛岀殑鏃堕棿銆傝繖鎰忓懗鐫€setTimeout鏈€澶氬彧鑳芥帹杩熸墽琛?147483647姣 锛?4.8澶╋級锛岃秴杩囪繖涓椂闂翠細鍙戠敓婧㈠嚭锛屽鑷村洖璋冨嚱鏁板皢鍦ㄥ綋鍓嶄换鍔¢槦鍒楃粨鏉熷悗绔嬪嵆鎵ц锛屽嵆绛夊悓浜巗etTimeout(f,0)鐨勬晥鏋溿€?/p>

4, setTimeout(f,0)搴旂敤

璋冩暣浜嬩欢鐨勫彂鐢熼『搴?/strong>

setTimeout(f,0)鏈夊嚑涓潪甯搁噸瑕佺殑鐢ㄩ€斻€傚畠鐨勪竴澶у簲鐢ㄦ槸锛屽彲浠ヨ皟鏁翠簨浠剁殑鍙戠敓椤哄簭銆傛瘮濡傦紝缃戦〉寮€鍙戜腑锛屾煇涓簨浠跺厛鍙戠敓鍦ㄥ瓙鍏冪礌锛岀劧鍚?鍐掓场鍒扮埗鍏冪礌锛屽嵆瀛愬厓绱犵殑浜嬩欢鍥炶皟鍑芥暟锛屼細鏃╀簬鐖跺厓绱犵殑浜嬩欢鍥炶皟鍑芥暟瑙﹀彂銆傚鏋滐紝鎴戜滑鍏堣鐖跺厓绱犵殑浜嬩欢鍥炶皟鍑芥暟鍏堝彂鐢燂紝灏辫鐢ㄥ埌 setTimeout(f, 0)銆?/p>

var input = document.getElementsByTagName(鈥榠nput[type=button]鈥?[0];

input.onclick = function A() {
  setTimeout(function B() {
    input.value +=鈥?input鈥?
  }, 0)
};

document.body.onclick = function C() {
  input.value += 鈥?body鈥?
};

涓婇潰浠g爜鍦ㄧ偣鍑绘寜閽悗锛屽厛瑙﹀彂鍥炶皟鍑芥暟A锛岀劧鍚庤Е鍙戝嚱鏁癈銆傚湪鍑芥暟A涓紝setTimeout灏嗗嚱鏁癇鎺ㄨ繜鍒颁笅涓€杞甃oop鎵ц锛岃繖鏍峰氨璧峰埌浜嗭紝鍏堣Е鍙戠埗鍏冪礌鐨勫洖璋冨嚱鏁癈鐨勭洰鐨勪簡銆?/p>

鐢ㄦ埛鑷畾涔夌殑鍥炶皟鍑芥暟锛岄€氬父鍦ㄦ祻瑙堝櫒鐨勯粯璁ゅ姩浣滀箣鍓嶈Е鍙戙€傛瘮濡傦紝鐢ㄦ埛鍦ㄨ緭鍏ユ杈撳叆鏂囨湰锛宬eypress浜嬩欢浼氬湪娴忚鍣ㄦ帴鏀舵枃鏈箣鍓嶈Е鍙戙€傚洜姝わ紝涓嬮潰鐨勫洖璋冨嚱鏁版槸杈句笉鍒扮洰鐨勭殑銆?/p>

document.getElementById(鈥榠nput-box鈥?.onkeypress = function(event) {
  this.value = this.value.toUpperCase();
}

涓婇潰浠g爜鎯冲湪鐢ㄦ埛杈撳叆鏂囨湰鍚庯紝绔嬪嵆灏嗗瓧绗﹁浆涓哄ぇ鍐欍€備絾鏄疄闄呬笂锛屽畠鍙兘灏嗕笂涓€涓瓧绗﹁浆涓哄ぇ鍐欙紝鍥犱负娴忚鍣ㄦ鏃惰繕娌℃帴鏀跺埌鏂囨湰锛屾墍浠his.value鍙栦笉鍒版渶鏂拌緭鍏ョ殑閭d釜瀛楃銆傚彧鏈夌敤setTimeout鏀瑰啓锛屼笂闈㈢殑浠g爜鎵嶈兘鍙戞尌浣滅敤銆?/p>

document.getElementById(鈥榤y-ok鈥?.onkeypress = function() {
  var self = this;
  setTimeout(function() {
    self.value = self.value.toUpperCase();
  }, 0);
}

涓婇潰浠g爜灏嗕唬鐮佹斁鍏etTimeout涔嬩腑锛屽氨鑳戒娇寰楀畠鍦ㄦ祻瑙堝櫒鎺ユ敹鍒版枃鏈箣鍚庤Е鍙?鍘熸潵濡傛锛?/strong>杩欎篃灏辫В閲婁簡缂樹綍鍦ㄤ娇鐢╞ackbone璋冪敤render涔嬫椂锛屾搷绾礵om鏄棤鏁堢殑浜嗭紝鍥犱负褰撴椂杩瀌om鍏冪礌閮借繕娌¤幏鍙栧埌(涓轰綍娌℃姤閿欙紵杩欑壍鎵埌鍙︿竴涓瘽棰?,鑷劧绛夐〉闈㈡覆鏌撳畬姣曚簡涔熸病瑙佹兂瑕佺殑缁撴灉浜嗐€?/p>

鍒嗗壊鑰楁椂浠讳綍

浼楁墍鍛ㄧ煡javascript鏄?strong>鍗曠嚎绋?/strong>鐨勶紝鐗圭偣灏辨槸瀹规槗鍑虹幇闃诲銆傚鏋滀竴娈电▼搴忓鐞嗘椂闂村緢闀匡紝寰堝鏄撳鑷存暣涓〉闈old浣忋€備粈涔堜氦浜掗兘澶勭悊涓嶄簡鎬庝箞鍔烇紵

绠€鍖栧鏉傚害锛熷鏉傞€昏緫鍚庣澶勭悊锛?span class="wp_keywordlink">html5鐨勫绾跨▼锛?.....

涓婇潰閮芥槸ok鐨勫仛娉曪紝浣嗘槸setTimeout涔熸槸澶勭悊杩欑闂鐨勪竴鎶婂ソ鎵嬨€俿etTimeout涓€涓緢鍏抽敭鐨勭敤娉曞氨鏄垎鐗囷紝濡傛灉涓€娈电▼搴忚繃澶э紝鎴?浠彲浠ユ媶鍒嗘垚鑻ュ共缁嗗皬鐨勫潡銆傜敱浜巗etTimeout(f,0)瀹為檯涓婃剰鍛崇潃锛屽皢浠诲姟鏀惧埌娴忚鍣ㄦ渶鏃╁彲寰楃殑绌洪棽鏃舵鎵ц锛屾墍浠ラ偅浜涜绠楅噺澶с€佽€楁椂闀跨殑浠?鍔★紝甯稿父浼氳鏀惧埌鍑犱釜灏忛儴鍒嗭紝鍒嗗埆鏀惧埌setTimeout(f,0)閲岄潰鎵ц(鍒嗙墖濉炲叆闃熷垪)锛岃繖鏍峰嵆浣垮湪澶嶆潅绋嬪簭娌℃湁澶勭悊瀹屾椂锛屾垜浠搷浣滈〉闈紝涔熸槸鑳?寰楀埌鍗虫椂鍝嶅簲鐨勩€傚叾瀹炲氨鏄皢浜や簰鎻掑叆鍒颁簡澶嶆潅绋嬪簭涓墽琛屻€?/p>

var div = document.getElementsByTagName(鈥榙iv鈥?[0];

// 鍐欐硶涓€
for(var i=0xA00000;i<0xFFFFFF;i++) {
  div.style.backgroundColor = 鈥?鈥?i.toString(16);
}

// 鍐欐硶浜?
var timer;
var i=0x100000;

function func() {
  timer = setTimeout(func, 0);
  div.style.backgroundColor = 鈥?鈥?i.toString(16);
  if (i++ == 0xFFFFFF) clearInterval(timer);
}

timer = setTimeout(func, 0);

涓婇潰浠g爜鏈変袱绉嶅啓娉曪紝閮芥槸鏀瑰彉涓€涓綉椤靛厓绱犵殑鑳屾櫙鑹层€傚啓娉曚竴浼氶€犳垚娴忚鍣?ldquo;鍫靛”锛岃€屽啓娉曚簩灏辫兘灏变笉浼氾紝杩欏氨鏄?strong>setTimeout(f,0)鐨勫ソ澶勩€傚嵆锛氬彲鍒╃敤setTimeout瀹炵幇涓€绉嶄吉澶氱嚎绋嬬殑姒傚康銆?/p>

鍙︿竴涓娇鐢ㄨ繖绉嶆妧宸х殑渚嬪瓙鏄紝浠g爜楂樹寒鐨勫鐞嗐€傚鏋滀唬鐮佸潡寰堝ぇ锛屽氨浼氬垎鎴愪竴涓釜灏忓潡锛屽啓鎴愯濡?strong>setTimeout(highlightNext, 50)鐨勬牱瀛愶紝杩涜鍒嗗潡澶勭悊銆?/p>

5, clearTimeout()

setTimeout鍜宻etInterval鍑芥暟锛岄兘杩斿洖涓€涓〃绀鸿鏁板櫒缂栧彿鐨勬暣鏁板€硷紝灏嗚鏁存暟浼犲叆clearTimeout鍜宑learInterval鍑芥暟锛屽氨鍙互鍙栨秷瀵瑰簲鐨勫畾鏃跺櫒銆?/p>

var id1 = setTimeout(f,1000);
var id2 = setInterval(f,1000);

clearTimeout(id1);
clearInterval(id2);

setTimeout鍜宻etInterval杩斿洖鐨勬暣鏁板€兼槸杩炵画鐨?涓€瀹氱幆澧冧笅锛屾瘮濡傛祻瑙堝櫒鎺у埗鍙帮紝鎴栬€?span class="wp_keywordlink">js鎵ц鐜绛?锛屼篃灏辨槸璇达紝绗簩涓猻etTimeout鏂规硶杩斿洖鐨勬暣鏁板€硷紝灏嗘瘮绗竴涓殑鏁存暟鍊煎ぇ1銆傚埄鐢ㄨ繖涓€鐐癸紝鍙互鍐欎竴涓嚱鏁帮紝鍙栨秷褰撳墠鎵€鏈夌殑setTimeout銆?/span>

(function() {
  var gid = setInterval(clearAllTimeouts, 0);

  function clearAllTimeouts() {
    var id = setTimeout(function() {}, 0);
    while (id > 0) {
      if (id !== gid) {
        clearTimeout(id);
      }
      id--;
    }
  }
})();

杩愯涓婇潰浠g爜鍚庯紝瀹為檯涓婂啀璁剧疆浠讳綍setTimeout閮芥棤鏁堜簡銆?/p>

涓嬮潰鏄竴涓猚learTimeout瀹為檯搴旂敤鐨勪緥瀛愩€傛湁浜涚綉绔欎細瀹炴椂灏嗙敤鎴峰湪鏂囨湰妗嗙殑杈撳叆锛岄€氳繃Ajax鏂规硶浼犲洖鏈嶅姟鍣紝jQuery鐨勫啓娉曞涓嬨€?/span>

$(鈥榯extarea鈥?.on(鈥榢eydown鈥? ajaxAction);

杩欐牱鍐欐湁涓€涓緢澶х殑缂虹偣锛屽氨鏄鏋滅敤鎴疯繛缁嚮閿紝灏变細杩炵画瑙﹀彂 keydown 浜嬩欢锛岄€犳垚澶ч噺鐨凙jax閫氫俊銆傝繖鏄笉蹇呰鐨勶紝鑰屼笖寰堝彲鑳戒細鍙戠敓鎬ц兘闂銆傛纭殑鍋氭硶搴旇鏄紝璁剧疆涓€涓棬妲涘€硷紝琛ㄧず涓ゆAjax閫氫俊鐨勬渶灏忛棿闅旀椂闂淬€傚鏋?鍦ㄨ瀹氱殑鏃堕棿鍐咃紝鍙戠敓鏂扮殑keydown浜嬩欢锛屽垯涓嶈Е鍙慉jax閫氫俊锛屽苟涓旈噸鏂板紑濮嬭鏃躲€傚鏋滆繃浜嗘寚瀹氭椂闂达紝娌℃湁鍙戠敓鏂扮殑keydown浜嬩欢锛屽皢杩涜 Ajax閫氫俊灏嗘暟鎹彂閫佸嚭鍘汇€?/p>

杩欑鍋氭硶鍙仛debounce锛堥槻鎶栧姩锛夋柟娉曪紝鐢ㄦ潵杩斿洖涓€涓柊鍑芥暟銆傚彧鏈夊綋涓ゆ瑙﹀彂涔嬮棿鐨勬椂闂撮棿闅斿ぇ浜庝簨鍏堣瀹氱殑鍊硷紝杩欎釜鏂板嚱鏁版墠浼氳繍琛屽疄闄呯殑浠诲姟銆傚亣瀹氫袱娆jax閫氫俊鐨勯棿闅斾笉灏忎簬2500姣锛屼笂闈㈢殑浠g爜鍙互鏀瑰啓鎴愪笅闈㈣繖鏍枫€?/p>

$(鈥榯extarea鈥?.on(鈥榢eydown鈥? debounce(ajaxAction, 2500))

鍒╃敤setTimeout鍜宑learTimeout锛屽彲浠ュ疄鐜癲ebounce鏂规硶銆傝鏂规硶鐢ㄤ簬闃叉鏌愪釜鍑芥暟鍦ㄧ煭鏃堕棿鍐呰瀵嗛泦璋冪敤锛屽叿浣撴潵璇达紝debounce鏂规硶杩斿洖涓€涓柊鐗堢殑璇ュ嚱鏁帮紝杩欎釜鏂扮増鍑芥暟璋冪敤鍚庯紝鍙湁鍦ㄦ寚瀹氭椂闂村唴娌℃湁鏂扮殑璋冪敤锛屾墠浼氭墽琛岋紝鍚﹀垯灏遍噸鏂拌鏃躲€?/p>

function debounce(fn, delay){
  var timer = null; // 澹版槑璁℃椂鍣?
  return function(){
    var context = this;
    var args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function(){
      fn.apply(context, args);
    }, delay);
  };
}

// 鐢ㄦ硶绀轰緥
var todoChanges = debounce(batchLog, 1000);
Object.observe(models.todo, todoChanges);

鐜板疄涓紝<span style="color:#ed1941">鏈€濂戒笉瑕佽缃お澶氫釜setTimeout鍜宻etInterval</span>锛屽畠浠?strong>鑰楄垂CPU銆傛瘮杈冪悊鎯崇殑鍋氭硶鏄紝灏嗚鎺ㄨ繜鎵ц鐨勪唬鐮侀兘鏀惧湪涓€涓嚱鏁伴噷锛岀劧鍚庡彧瀵硅繖涓嚱鏁颁娇鐢╯etTimeout鎴杝etInterval銆?/p>

6, 濡備綍浣跨敤setTimeout

瀵?strong>setTimeout鑷劧涓嶆浜庤繖浜涳紝浣嗗凡瓒宠鍏跺己澶с€傞偅涔堥棶棰樻潵浜嗭紝闇€瑕佸湪椤圭洰涓ぇ閲忎娇鐢ㄤ箞锛熻涓汉鍜岄」鐩?鑰屽畾鍚э紱濡備笉鑳界啛缁冩帉鎻★紝涓嶅缓璁鐢ㄣ€傛瘯绔熷湪鏌愪簺鎯呮櫙涔嬩笅锛宻etTimeout浣滀负涓€涓猦ack鐨勬柟寮忚€屽瓨鍦ㄧ殑锛堟墦涔辨ā鍧楃殑鐢熷懡鍛ㄦ湡锛屽苟涓斿湪闂鍑虹幇鏃?寰堥毦璋冭瘯锛屼綘鎳傜殑锛夛紝璀锛氬綋涓€涓疄渚嬭繕娌℃湁鍒濆鍖栫殑鍓嶏紝鎴戜滑灏变娇鐢ㄨ繖涓疄渚嬶紝閿欒鐨勮В鍐冲姙娉曟槸浣跨敤瀹炰緥鏃跺姞涓猻etTimeout锛岀‘淇濆疄渚嬩娇鐢ㄥ墠宸插垵 濮嬪寲銆?/p>

浣嗗彧瑕佽冻澶?strong>鐔熸倝瀹冿紝浠ュ強浣跨敤鐨勫満鏅?鍖呮嫭妯″潡鐢熷懡鍛ㄦ湡)锛屼娇鐢ㄥ畠涔熷氨鏃犲彲鍘氶潪浜嗐€傛瘮濡倁nderscore涓笉灏戞柟娉曚篃鏄熀浜庤繖setTimeout鏂规硶鍐欑殑锛涙瘮濡傞潪甯稿己澶х殑_.defer锛?寤惰繜璋冪敤function鐩村埌褰撳墠璋冪敤鏍堟竻绌轰负姝紝绫讳技浣跨敤寤舵椂涓?鐨剆etTimeout鏂规硶銆傚浜庢墽琛屽紑閿€澶х殑璁$畻鍜屾棤闃诲UI绾跨▼鐨凥TML娓叉煋鏃?鍊欓潪甯告湁鐢ㄣ€?濡傛灉浼犻€抋rguments鍙傛暟锛屽綋鍑芥暟function鎵ц鏃讹紝 arguments 浼氫綔涓哄弬鏁颁紶鍏ャ€?/p>

涔熸瘮濡傚墠鏂囨彁鍒扮殑闃叉姈鍔ㄦ柟娉昫ebounce锛涘湪underscore涓紙_.debounce(function, wait, [immediate])锛夊叾瀹炵幇鏂规硶濡備笅锛?/p>

  _.debounce = function(func, wait, immediate) {
    var timeout, args, context, timestamp, result;

    var later = function() {
      var last = _.now() - timestamp;

      if (last < wait && last >= 0) {
        timeout = setTimeout(later, wait - last);
      } else {
        timeout = null;
        if (!immediate) {
          result = func.apply(context, args);
          if (!timeout) context = args = null;
        }
      }
    };

    return function() {
      context = this;
      args = arguments;
      timestamp = _.now();
      var callNow = immediate && !timeout;
      if (!timeout) timeout = setTimeout(later, wait);
      if (callNow) {
        result = func.apply(context, args);
        context = args = null;
      }

      return result;
    };
  };

鍙傝€冩枃绔犻摼鎺ワ細
闃竴宄?娴忚鍣ㄥ璞?瀹氭椂鍣?/a>
浣犳墍涓嶄簡瑙g殑setTimeout

以上是关于鍏充簬setTimeout()浣犳墍涓嶇煡閬撶殑鍦版柟的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯

姣忎竴涓▼搴忓憳閮藉簲璇ョ煡閬撶殑楂樺苟鍙戝鐞嗘妧宸с€佸垱涓氬叕鍙稿浣曡В鍐抽珮骞跺彂闂銆佷簰鑱旂綉楂樺苟鍙戦棶棰樿В鍐虫€濊矾銆乧aoz澶х澶氬勾缁忛獙鎬荤粨鍒嗕韩

宸ヤ綔涓や笁骞翠簡锛屾暣涓嶆槑鐧芥灦鏋勫浘閮界敾鍟ワ紵

鍏充簬瀵规媿

鍏充簬HTTP璇锋眰

鍏充簬javascript涓殑dataset