javascript javascript中事件处理程序的异步或同步调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15924014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asynchronous or Synchronous calling of event handlers in javascript
提问by sachinjain024
Are event handlers executed synchronously or asynchronously in JavaScript? Here is JS binwhich is showing that event handler is executed synchronously.
JavaScript 中的事件处理程序是同步执行还是异步执行?这是JS bin,它显示事件处理程序是同步执行的。
Code:
代码:
$('#toclick').bind('custom', function() {
for (var i=0; i<100000; i++) {}
console.log('Inside click handler');
});
$('#toclick').trigger('custom');
console.log('Outside click handler');
Output:
输出:
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 ?
这意味着如果我们触发一个事件,除非所有事件处理程序都被执行,否则它下面的代码不会被执行。我对吗 ?
采纳答案by freakish
That's correct. All event handlers are fired synchronously and in order of binding.
没错。所有的事件处理程序都是按绑定顺序同步触发的。
回答by Dimitris Zorbas
Some event handlers are executed synchonously and others asynchronously. See DOM-Level-3-Events
一些事件处理程序是同步执行的,而另一些则是异步执行的。见DOM-Level-3-Events