本文介紹了javascript中事件處理程序的異步或同步調用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在 JavaScript 中事件處理程序是同步執行還是異步執行?這是 JS bin 顯示事件處理程序是同步執行的.
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
這意味著如果我們觸發一個事件,它下面的代碼將不會被執行,除非所有的事件處理程序都被執行.我說的對嗎?
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 ?
帶有多個事件處理程序的Bin
推薦答案
沒錯.所有事件處理程序都是按綁定順序同步觸發的.
That's correct. All event handlers are fired synchronously and in order of binding.
這篇關于javascript中事件處理程序的異步或同步調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!