how do I prevent event handlers to handle multiple events at once in javascript?

↘锁芯ラ 提交于 2019-12-11 08:23:41

问题


How do I prevent JavaScript from handling two events at once? I have an event handler declared such as document.addEventListener('keydown',function(e) {},false) in this case, I do not want the handler to handle any events until it has finished with the current one.


回答1:


Javascript is single threaded so the current event processing will finish before the next event is triggered. You do not have to do anything to guarantee that only one event is processed at a time.

You can learn more about this topic from these other posts:

How does JavaScript handle AJAX responses in the background? (describes how event queues in javascript operate)

Can JS event handlers interrupt execution of another handler?

Race conditions with JavaScript event handling?




回答2:


There is nothing as simultaneous event handling. Event order depends upon which browser is being used. It is you who decides how to register an event either in capture or bubbling model or use both theses in w3c model.

For example in W3c model any event taking place in the W3c model is captured till the target is reached, and then it starts bubbling up again.

You can decide to register your event in either capturing or bubbling phases through the addEventListner() method.



来源:https://stackoverflow.com/questions/9009943/how-do-i-prevent-event-handlers-to-handle-multiple-events-at-once-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!