1

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.

fenerlitk
  • 5,414
  • 9
  • 29
  • 39

2 Answers2

3

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?

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

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.

Rahul
  • 1,549
  • 3
  • 17
  • 35