本文介紹了javascript中事件處理程序的異步或同步調(diào)用的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
在 JavaScript 中事件處理程序是同步執(zhí)行還是異步執(zhí)行?這是 JS bin 顯示事件處理程序是同步執(zhí)行的.
Are event handlers executed synchronously or asynchronously in JavaScript? Here is JS bin which is showing that event handler is executed synchronously.
代碼:
$('#toclick').bind('custom', function() {
for (var i=0; i<100000; i++) {}
console.log('Inside click handler');
});
$('#toclick').trigger('custom');
console.log('Outside click handler');
輸出:
Inside click handler
Outside click handler
這意味著如果我們觸發(fā)一個(gè)事件,它下面的代碼將不會(huì)被執(zhí)行,除非所有的事件處理程序都被執(zhí)行.我說(shuō)的對(duì)嗎?
This means if we trigger an event, the code below it won't be executed unless all the event handlers are executed. Am I right ?
帶有多個(gè)事件處理程序的Bin
推薦答案
沒(méi)錯(cuò).所有事件處理程序都是按綁定順序同步觸發(fā)的.
That's correct. All event handlers are fired synchronously and in order of binding.
這篇關(guān)于javascript中事件處理程序的異步或同步調(diào)用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!